题目:

 

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. 证书锁定Certificate Pinning技术

    证书锁定Certificate Pinning技术   在中间人攻击中,攻击主机通常截断客户端和服务器的加密通信.攻击机以自己的证书替代服务器发给客户端的证书.通常,客户端不会验证该证书,直接接受该证 ...

  2. spring版本不兼容JDK问题

    在实验书上Spring项目的时候出现一个问题,导入包和使用注释的时候eclipse出现报错. 导入包报错:The import org cannot be resolved 注释报错:componen ...

  3. 554 DT:SPM 163 smtp5,D9GowAD3RPYqSvxZjpMaAA--.4817S2 1509706293 坑爹的防垃圾邮件机制

    代码如下 package ssmtest; import java.io.File;import java.io.UnsupportedEncodingException;import java.ut ...

  4. Java获取指定时间(转)

    说明:从LocalDate的API上看,主要用于快速获取当前年月日,而DateFormatter也基本上伴随着使用.如果是操作Date对象的,主要是用于时间戳等,伴随着使用的是SimpleDateFo ...

  5. P2P技术简介(包括BT软件的分析)(转)

    这是一篇别人发表的论文,里面很全面的解释了P2P技术的实现,以及BT网络中应用P2P技术所设计的原理,并列举BT软件的一些专业名词的定义.由于论文发表的比较早,2005年时还没有DHT技术. (链接: ...

  6. MFC改变控件颜色

    from http://www.cppblog.com/FandyM/archive/2010/07/21/120972.aspx MFC应用程序中,要改变控件的背景色可通过重载OnCtlColor( ...

  7. HTML5 Canvas实现360度全景图

    原文:http://blog.csdn.net/jia20003/article/details/17172571 很多购物网站现在都支持360实物全景图像,可以360度任意选择查看样品,这样 对购买 ...

  8. iOS开发 ----- 加载动画之牛顿摆的实现

    牛顿摆动画 自己看动画有一段时间了,但是还是不是很能理解其中的一些属性方法之类的东西,琢磨了一下午写了一个牛顿摆的动画,这里记录一下,一遍以后查看先上图 先说下思路 说下牛顿摆的大致运动过程 根据牛顿 ...

  9. mac异常删除管理员账户恢复操作

    重新启动电脑,同时按下command+s键进入命令行方式 待系统加载完成后顺序输入以下命令: /sbin/mount -uaw rm var/db/.applesetupdone reboot 待系统 ...

  10. javascript原生调用摄像头

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...