Kattis-What does the fox say 字符串处理+STL
題目
The forest is, however, full of animals’ voices, and on your recording, many different sounds can be heard. But you are well prepared for your task: you know exactly all the sounds which other animals make. Therefore the rest of the recording—all the unidentified noises—must have been made by the fox.
Input
The first line of input contains the number of test cases T. The descriptions of the test cases follow:
The first line of each test case contains the recording—words over lower case English alphabet, separated by spaces. Each contains at most 100 letters and there are no more than 100 words. The next few lines are your pre-gathered information about other animals, in the format goes . There are no more than 100 animals, their names are not longer than 100 letters each and are actual names of animals in English. There is no fox goes … among these lines.
The last line of the test case is exactly the question you are supposed to answer: what does the fox say?
Output
For each test case, output one line containing the sounds made by the fox, in the order from the recording. You may assume that the fox was not silent (contrary to popular belief, foxes do not communicate by Morse code).
思路
使用vector把第一行叫聲存入 再用set標記出來每個animal的叫聲,然后判斷是否出現即可。
問題在于字符串的處理。
第一。
While(cin>>temp)可以連續輸入
退出的條件是:1.無效輸入,比如類型不同;2.遇到文件結束符(End_Of_FIle)。
第二。出現第一個goes作為第一行截止標記。然后刪除goes前面的那個元素即可。
讀入第一行叫聲的代碼實現
while(cin>>temp){//輸入第一行 if(temp=="goes")//后面肯定有一個輸入,第一個叫聲 {cin>>temp;s.insert(temp);//存進來v.pop_back();//goes之前有個無用的字符,刪除 break;}v.push_back(temp);//進入向量 }代碼
#include<bits/stdc++.h>using namespace std;int main() {int t;cin>>t;getchar();while(t--){vector<string> v;//存入第一行的叫聲 set<string> s;//標記下面的叫聲 string temp;while(cin>>temp){//輸入第一行 if(temp=="goes")//后面肯定有一個輸入,第一個叫聲 {cin>>temp;s.insert(temp);//存進來v.pop_back();//goes之前有個無用的字符,刪除 break;}v.push_back(temp);//進入向量 }string animal,sound;//讀取animal goes sound while(cin>>animal>>temp>>sound){if(temp!="goes")//最有一句輸入沒用,有五個字符,全部吃掉{cin>>temp>>animal;break; }s.insert(sound);//sound 進入set用作判斷 }for(size_t i=0;i<v.size();++i){if(s.find(v[i])==s.end())//在s中沒有找到則是fox 的叫聲 {cout<<v[i];if(i==v.size()-1) cout<<endl;else cout<<" ";}}}return 0; }總結
以上是生活随笔為你收集整理的Kattis-What does the fox say 字符串处理+STL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kattis ones简单题取模运算+枚
- 下一篇: 水题Kattis Temperature