题目:

 

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 1 Sample Output 1
  1. 1
  2. toot woof wa ow ow ow pa blub blub pa toot pa blub pa pa ow pow toot
  3. dog goes woof
  4. fish goes blub
  5. elephant goes toot
  6. seal goes ow
  7. what does the fox say?
  1. 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[],a[];
  13. char voice[];
  14. char m[][];
  15.  
  16. int main()
  17. {
  18. int t,vsum,len,cnt;
  19. scanf("%d",&t);
  20. getchar();
  21. while(t--)
  22. {
  23. vsum = ;
  24. gets(s);//读取动物的叫声
  25. while()
  26. {
  27. gets(a);
  28. if(!strcmp(a,"what does the fox say?")) break;
  29.  
  30. len = strlen(a);
  31. cnt = ;
  32. for(int i = len-; a[i]!=' '; i--)//倒着读取动物声音,跳过无用信息
  33. voice[cnt++] = a[i];
  34. voice[cnt] = ;
  35.  
  36. char tmp;//处理动物声音
  37. len = strlen(voice);
  38. for(int i = ; i<=(len-)/; i++)//把动物声音调回正序
  39. {
  40. tmp = voice[i]; voice[i] = voice[len--i]; voice[len--i] = tmp;
  41. }
  42.  
  43. strcpy(m[vsum],voice);//将非狼声音放到队列中
  44. vsum++;
  45. }
  46.  
  47. cnt = ;
  48. s[strlen(s)] = ' ';
  49. s[strlen(s)] = ;
  50. for(int i = ; s[i]!=; i++)
  51. {
  52. if(s[i]==' ')
  53. {
  54. voice[cnt] = ;
  55. cnt = ;
  56.  
  57. int j;
  58. for(j = ; j<vsum; j++)
  59. if(strcmp(voice,m[j])==) 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 ;
  72. }

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[],s[];//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()
  27. {
  28. gets(a);
  29. if(!strcmp(a,"what does the fox say?")) break;
  30.  
  31. voice = "";
  32. for(int i = strlen(a)-; a[i]!=' '; i--)//倒着读取动物声音,跳过无用信息
  33. voice += a[i];
  34.  
  35. char tmp;
  36. for(int i = ,len = voice.size(); i<=(len-)/; i++)//把动物声音调回正序
  37. {
  38. tmp = voice[i]; voice[i] = voice[len--i]; voice[len--i] = tmp;
  39. }
  40.  
  41. m.insert(voice);
  42. }
  43.  
  44. s[strlen(s)] = ' ';
  45. s[strlen(s)] = ;
  46. voice = "";
  47. for(int i = ,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 ;
  62. }

Kattis - whatdoesthefoxsay —— 字符串的更多相关文章

  1. Kattis - virus【字符串】

    Kattis - virus[字符串] 题意 有一个正常的DNA序列,然后被病毒破坏.病毒可以植入一段DNA序列,这段插入DNA序列是可以删除正常DNA序列中的一个连续片段的. 简单来说就是,给你一段 ...

  2. Kattis - names Palindrome Names 【字符串】

    题目链接 https://open.kattis.com/problems/names 题意 给出一个字符串 有两种操作 0.在字符串的最末尾加一个字符 1.更改字符串中的一个字符 求最少的操作步数使 ...

  3. Kattis - prva 【字符串】

    题意 从上到下 或者 从左到右 组成的长度 >= 2 的字符串 如果遇到 # 就断掉 输出 字典序最小的那一个 思路 只要从上到下 和从左到右 分别遍历一遍,将 长度 >= 2 的字符串 ...

  4. Kattis - yoda 【字符串】

    分析 给出两个串 从末尾开始对齐 每位对齐后,每一位 遍历 如果 第一串 的那位 < 第二串 的 那么 第一串的那位 就删去 如果 等于 两位 都保留 如果 大于 那么 保留 第二串的 那位 如 ...

  5. Subsequences in Substrings Kattis - subsequencesinsubstrings (暴力)

    题目链接: Subsequences in Substrings Kattis - subsequencesinsubstrings 题目大意:给你字符串s和t.然后让你在s的所有连续子串中,找出这些 ...

  6. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  7. 测试一下StringBuffer和StringBuilder及字面常量拼接三种字符串的效率

    之前一篇里写过字符串常用类的三种方式<java中的字符串相关知识整理>,只不过这个只是分析并不知道他们之间会有多大的区别,或者所谓的StringBuffer能提升多少拼接效率呢?为此写个简 ...

  8. java中的字符串相关知识整理

    字符串为什么这么重要 写了多年java的开发应该对String不陌生,但是我却越发觉得它陌生.每学一门编程语言就会与字符串这个关键词打不少交道.看来它真的很重要. 字符串就是一系列的字符组合的串,如果 ...

  9. JavaScript 字符串实用常操纪要

    JavaScript 字符串用于存储和处理文本.因此在编写 JS 代码之时她总如影随形,在你处理用户的输入数据的时候,在读取或设置 DOM 对象的属性时,在操作 Cookie 时,在转换各种不同 Da ...

随机推荐

  1. app 检查更新和更新

    第一种,手动检查 ////  Harpy.h//  Harpy////  Created by Arthur Ariel Sabintsev on 11/14/12.//  Copyright (c) ...

  2. 卸载ArcGISDesktop低版本程序遇到异常,如何完全卸载?

    [解决办法]:正常情况下,运行 ArcGIS for Desktop 光盘中的 “冲突检测”工具,会自动完全卸载低版本的ArcGIS 程序.如果遇到异常无法卸载(例如安装过非正式版软件),如下位置是A ...

  3. 细说Redis持久化机制

    概述 Redis不仅能够作为缓存来使用,也能够作为内存数据库. Redis作为内存数据库使用时.必需要解决一个问题:数据的持久性.有些将Redis作为缓存使用的场景也需要将缓存的数据持久化到存储介质上 ...

  4. Linux中的热键[Tab] [Ctrl]-c [Ctrl]-d

    Tab键:命令或者文件补全.可以避免很多的输入错误 1. 按一次,文件或命令补全 2. 按两次,会列举出以按键前的字母为首的所有命令或者文件 Ctrl+C:中断目前程序 Ctrl+D:键盘输入结束.可 ...

  5. js利用offsetWidth和clientWidth来计算滚动条的宽度

    原文: http://www.haorooms.com/post/js_scroll_width 参考: https://www.cnblogs.com/benxiaohai-microcosm/p/ ...

  6. Hibernate中的条件查询完毕类

    Hibernate中的条件查询有下面三个类完毕: 1.Criteria:代表一次查询 2.Criterion:代表一个查询条件 3.Restrictions:产生查询条件的工具类

  7. 使用excel进行数据挖掘(5)---- 应用场景分析

    使用excel进行数据挖掘(5)---- 应用场景分析 在配置环境后,能够使用excel进行数据挖掘. 环境配置问题可參阅: http://blog.csdn.net/xinxing__8185/ar ...

  8. bootstrap-data-target触发模态弹出窗元素的data使用 data-toggle与data-target的作用 深入ASP.NET MVC之九:Ajax支持 Asp.Net MVC4系列--进阶篇之AJAX

    bootstrap-data-target触发模态弹出窗元素的data使用 时间:2017-05-27 14:22:34      阅读:4479      评论:0      收藏:0      [ ...

  9. 找回Xcode7的代码折叠功能

    升级到Xcode7后,会发现代码折叠功能不见了,这是怎么回事? 其实这个功能还在的,用以下的快捷键仍然可以折叠代码.只是不能用鼠标实现折叠了:在Xcode菜单里选择Preference——Text E ...

  10. strupr和strlwr字符串函数的使用

    strupr 功能:将小写字母转换为大写字母 strlwr 功能:将大写字母转换为小写字母 在VS2013里面使用的时候要这样的格式 _strlwr_s _strupr_s #include<s ...