Kattis - whatdoesthefoxsay —— 字符串

题目:

 

 

Determined to discover the ancient mystery—the sound that the fox makes—you went into the forest, armed with a very good digital audio recorder. 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 TT. 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 <animal> goes <sound>. 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).

Sample Input 1Sample Output 1
1
toot woof wa ow ow ow pa blub blub pa toot pa blub pa pa ow pow toot
dog goes woof
fish goes blub
elephant goes toot
seal goes ow
what does the fox say?
wa pa pa pa pa pa pow

 

 


题解:

难度不大,就是如果用C写的话,处理字符串时很烦,细节要搞好。

把其他动物的叫声放到一个队列里,对于那堆叫声,如果不能在队列里找到,则证明是狼的叫声,直接输出。


C代码如下:

 1 #include<cstdio>//E - E Kattis - whatdoesthefoxsay
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<set>
 7 
 8 using namespace std;
 9 
10 typedef long long ll;
11 
12 char s[250],a[250];
13 char voice[250];
14 char m[250][250];
15 
16 int main()
17 {
18     int t,vsum,len,cnt;
19     scanf("%d",&t);
20     getchar();
21     while(t--)
22     {
23         vsum = 0;
24         gets(s);//读取动物的叫声
25         while(1)
26         {
27             gets(a);
28             if(!strcmp(a,"what does the fox say?")) break;
29 
30             len = strlen(a);
31             cnt = 0;
32             for(int i = len-1; a[i]!=' '; i--)//倒着读取动物声音,跳过无用信息
33                 voice[cnt++] = a[i];
34             voice[cnt] = 0;
35 
36             char tmp;//处理动物声音
37             len = strlen(voice);
38             for(int i = 0; i<=(len-1)/2; i++)//把动物声音调回正序
39             {
40                 tmp = voice[i]; voice[i] = voice[len-1-i]; voice[len-1-i] = tmp;
41             }
42 
43             strcpy(m[vsum],voice);//将非狼声音放到队列中
44             vsum++;
45         }
46 
47         cnt = 0;
48         s[strlen(s)] = ' ';
49         s[strlen(s)] = 0;
50         for(int i = 0; s[i]!=0; i++)
51         {
52             if(s[i]==' ')
53             {
54                 voice[cnt] = 0;
55                 cnt = 0;
56 
57                 int j;
58                 for(j = 0; j<vsum; j++)
59                 if(strcmp(voice,m[j])==0) break;
60 
61                 if(j==vsum)
62                     printf("%s ",voice);
63             }
64 
65             else
66                 voice[cnt++] = s[i];
67         }
68         putchar('\n');
69     }
70 
71     return 0;
72 }
View Code

 

C++代码如下(使用STL则简便多了):

 1 #include<iostream>//E - E Kattis - whatdoesthefoxsay
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<set>
 8 #include<string>
 9 #include<set>
10 #define LL long long
11 using namespace std;
12 
13 char a[2500],s[2500];//C语言开250可以过,为什么C++就不行了?而要开2500
14 set<string>m;
15 string voice;
16 
17 int main()
18 {
19     int t;
20     scanf("%d",&t);
21     getchar();
22     while(t--)
23     {
24         gets(s);
25         m.clear();
26         while(1)
27         {
28             gets(a);
29             if(!strcmp(a,"what does the fox say?")) break;
30 
31             voice = "";
32             for(int i = strlen(a)-1; a[i]!=' '; i--)//倒着读取动物声音,跳过无用信息
33                 voice += a[i];
34 
35             char tmp;
36             for(int i = 0,len = voice.size(); i<=(len-1)/2; i++)//把动物声音调回正序
37             {
38                 tmp = voice[i]; voice[i] = voice[len-1-i]; voice[len-1-i] = tmp;
39             }
40 
41             m.insert(voice);
42         }
43 
44         s[strlen(s)] = ' ';
45         s[strlen(s)] = 0;
46         voice = "";
47         for(int i = 0,len = strlen(s); i<len; i++)
48         {
49             if(s[i]==' ')
50             {
51                 if(m.find(voice)==m.end())
52                     cout<<voice<<' ';
53                 voice = "";
54             }
55 
56             else
57                 voice += s[i];
58         }
59         putchar('\n');
60     }
61     return 0;
62 }
View Code

 

posted on 2017-03-24 16:16  h_z_cong  阅读(436)  评论(0编辑  收藏  举报

导航