题目:

 

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
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代码如下:

 #include<cstdio>//E - E Kattis - whatdoesthefoxsay
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<set> using namespace std; typedef long long ll; char s[],a[];
char voice[];
char m[][]; int main()
{
int t,vsum,len,cnt;
scanf("%d",&t);
getchar();
while(t--)
{
vsum = ;
gets(s);//读取动物的叫声
while()
{
gets(a);
if(!strcmp(a,"what does the fox say?")) break; len = strlen(a);
cnt = ;
for(int i = len-; a[i]!=' '; i--)//倒着读取动物声音,跳过无用信息
voice[cnt++] = a[i];
voice[cnt] = ; char tmp;//处理动物声音
len = strlen(voice);
for(int i = ; i<=(len-)/; i++)//把动物声音调回正序
{
tmp = voice[i]; voice[i] = voice[len--i]; voice[len--i] = tmp;
} strcpy(m[vsum],voice);//将非狼声音放到队列中
vsum++;
} cnt = ;
s[strlen(s)] = ' ';
s[strlen(s)] = ;
for(int i = ; s[i]!=; i++)
{
if(s[i]==' ')
{
voice[cnt] = ;
cnt = ; int j;
for(j = ; j<vsum; j++)
if(strcmp(voice,m[j])==) break; if(j==vsum)
printf("%s ",voice);
} else
voice[cnt++] = s[i];
}
putchar('\n');
} return ;
}

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

 #include<iostream>//E - E Kattis - whatdoesthefoxsay
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<set>
#include<string>
#include<set>
#define LL long long
using namespace std; char a[],s[];//C语言开250可以过,为什么C++就不行了?而要开2500
set<string>m;
string voice; int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
gets(s);
m.clear();
while()
{
gets(a);
if(!strcmp(a,"what does the fox say?")) break; voice = "";
for(int i = strlen(a)-; a[i]!=' '; i--)//倒着读取动物声音,跳过无用信息
voice += a[i]; char tmp;
for(int i = ,len = voice.size(); i<=(len-)/; i++)//把动物声音调回正序
{
tmp = voice[i]; voice[i] = voice[len--i]; voice[len--i] = tmp;
} m.insert(voice);
} s[strlen(s)] = ' ';
s[strlen(s)] = ;
voice = "";
for(int i = ,len = strlen(s); i<len; i++)
{
if(s[i]==' ')
{
if(m.find(voice)==m.end())
cout<<voice<<' ';
voice = "";
} else
voice += s[i];
}
putchar('\n');
}
return ;
}

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. BZOJ——1601: [Usaco2008 Oct]灌水

    http://www.lydsy.com/JudgeOnline/problem.php?id=1601 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit:  ...

  2. 两种const函数

    有两种const函数,声明如下:1.const T func();2.T func() const;第一种表示返回的是const的类型,也即返回的值不能作为左值,楼主懂的.第二种表示该成员函数不能修改 ...

  3. Codeforces Gym 100203I I WIN 最大流

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 首先寻找每个I,然后枚举形状,如果匹 ...

  4. 品质与合身 无须昂贵 | Tailorwoods在线男装定制

    品质与合身 无须昂贵 | Tailorwoods在线男装定制 北京市朝阳区姚家园北一路八月照相馆2F

  5. Eval is evil ('Eval是魔鬼') --- eval()函数

    Eval is evil ('Eval是魔鬼') eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码.语法:eval(string): eval的使用场合是什么呢?有时候我 ...

  6. Delphi TScrollBar 用于滚动窗口、组件内容

    滚动条组件(TScrollBar)此组件是一个Windows滚动条,用于滚动窗口.组件内容.许多控制有滚动条属性,它们把滚动条作为自己的一部分,对于没有完整滚动条的控制,TScrollBar组件提供了 ...

  7. Android API Guides---Services

    服务 在该文献 基础 声明在清单服务 创建一个启动的服务 扩展IntentService类 扩展服务类 启动服务 停止服务 创建绑定服务 将通知发送给用户 执行在前台服务 管理服务生命周期 实施生命周 ...

  8. hdu 5389 Zero Escape (dp)

    题目:http://acm.hdu.edu.cn/showproblem.php? pid=5389 题意:定义数根:①把每一位上的数字加起来得到一个新的数,②反复①直到得到的数仅仅有1位.给定n,A ...

  9. 可空类型Nullable

    Nullable类型: 值类型变量默认为0,不可空,为了使它可空,出现了Nullable类型,类型前面加?  变为引用类型 值类型是没有null值的,比如int,DateTime,它们都有默认值.举个 ...

  10. Flash制作和软件使用

    Flash制作和软件使用 2014-11-09 ——君子善假于物也 引子 虽说FLASH在随着HTML5的发展而受阻,尤其移动终端都不再支持它了,但是在一段时间内还是重要的.近期朋友说要结婚,想弄个电 ...