ZeroJudge C381: 聖經密碼

題目敘述

本題採EOF方式收資料,每筆資料第一行會有兩個正整數N和M,當N和M等於0時停止收資料。接下來會有N行,每行有一個英文單字,接下來會有一行包含M個以空格分開的正整數。要求將N個英文單字組成一整個字串時輸出每個數字給的字串位置的字元。


範例輸入

9 8

the

quick

brown

fox

jumps

over

the

lazy

dog

33 11 34 19 21 33 30 32

0 0

範例輸出

doomsday


解題思路

將所有英文單字加到一個字串之後,在收數字時就將字串的那一位-1 (因為測資的數字是1-Based) 輸出即可,最後換行。

解題程式碼如下 (僅供參考):

#include <iostream>
using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M)
    {
      if (N == 0 && M == 0) break;
      string word = "";
      for (int i = 0; i<N; i++)
        {
          string str;
          cin >> str;
          word += str;
        }
      for (int i = 0; i<M; i++)
        {
          int tmp;
          cin >> tmp;
          cout << word[tmp-1];
        }
      cout << "\n";
    }
}

留言

這個網誌中的熱門文章

ZeroJudge M933: 邏輯電路

ZeroJudge A148: You Cannot Pass?!

ZeroJudge A263: 日期差幾天