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