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 1 | Sample Output 1 |
|---|---|
1 |
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 —— 字符串的更多相关文章
- Kattis - virus【字符串】
Kattis - virus[字符串] 题意 有一个正常的DNA序列,然后被病毒破坏.病毒可以植入一段DNA序列,这段插入DNA序列是可以删除正常DNA序列中的一个连续片段的. 简单来说就是,给你一段 ...
- Kattis - names Palindrome Names 【字符串】
题目链接 https://open.kattis.com/problems/names 题意 给出一个字符串 有两种操作 0.在字符串的最末尾加一个字符 1.更改字符串中的一个字符 求最少的操作步数使 ...
- Kattis - prva 【字符串】
题意 从上到下 或者 从左到右 组成的长度 >= 2 的字符串 如果遇到 # 就断掉 输出 字典序最小的那一个 思路 只要从上到下 和从左到右 分别遍历一遍,将 长度 >= 2 的字符串 ...
- Kattis - yoda 【字符串】
分析 给出两个串 从末尾开始对齐 每位对齐后,每一位 遍历 如果 第一串 的那位 < 第二串 的 那么 第一串的那位 就删去 如果 等于 两位 都保留 如果 大于 那么 保留 第二串的 那位 如 ...
- Subsequences in Substrings Kattis - subsequencesinsubstrings (暴力)
题目链接: Subsequences in Substrings Kattis - subsequencesinsubstrings 题目大意:给你字符串s和t.然后让你在s的所有连续子串中,找出这些 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- 测试一下StringBuffer和StringBuilder及字面常量拼接三种字符串的效率
之前一篇里写过字符串常用类的三种方式<java中的字符串相关知识整理>,只不过这个只是分析并不知道他们之间会有多大的区别,或者所谓的StringBuffer能提升多少拼接效率呢?为此写个简 ...
- java中的字符串相关知识整理
字符串为什么这么重要 写了多年java的开发应该对String不陌生,但是我却越发觉得它陌生.每学一门编程语言就会与字符串这个关键词打不少交道.看来它真的很重要. 字符串就是一系列的字符组合的串,如果 ...
- JavaScript 字符串实用常操纪要
JavaScript 字符串用于存储和处理文本.因此在编写 JS 代码之时她总如影随形,在你处理用户的输入数据的时候,在读取或设置 DOM 对象的属性时,在操作 Cookie 时,在转换各种不同 Da ...
随机推荐
- SSL剥离工具sslstrip
SSL剥离工具sslstrip 在日常上网过程中,用户只是在地址栏中输入网站域名,而不添加协议类型,如HTTP和HTTPS.这时,浏览器会默认在域名之前添加http://,然后请求网站.如果网站采 ...
- 济南day4
啥也不会,做了不对,对了没分.T1 50 + 30 + 0想了想,有思路,写了,半个小时写完,算错复杂度,复杂度最差(n*m),想成了(n+m)被卡没了50分,gg.....T2自己写了个单向并查集, ...
- C# SQL帮助类
C# SQL帮助类 本人自己封装的SQLHelper类,执行sql server与Oracle数据的增删改查 vs自带的Oracle数据库引用需要安装Oracle客户端,如不想安装Oracle客户端, ...
- Hibernate注解详解
一.实体Bean 每个持久化POJO类都是一个实体Bean, 通过在类的定义中使用 @Entity 注解来进行声明. 声明实体Bean @Entitypublic class Flightimplem ...
- 如何使用ssh远程编辑定时任务crontab?
linxu定时任务使用crontab,编辑crontab可以直接编辑:crontab -e:也可以直接读取文件 crontab file.这两种操作都不需要特殊权限sudo.区别在于,crontab ...
- repeated-substring-pattern
https://leetcode.com/problems/repeated-substring-pattern/ 下面这个方法,开始我觉得挺好.可惜还是超时了.后来我就加了一个剪枝策略,只有长度能够 ...
- Tensorflow 之 TensorBoard可视化Graph和Embeddings
windows下使用tensorboard tensorflow 官网上的例子程序都是针对Linux下的:文件路径需要更改 tensorflow1.1和1.3的启动方式不一样 :参考:Running ...
- 网页Tab控件
网页Tab控件 找到:http://www.open-open.com/ajax/2_Tabs.htm 页面,查看了若干Tab控件, 找到了:http://www.open-open.com/ajax ...
- maven编译maven-surefire-plugin插件报错
1.编译错误信息: [INFO] ------------------------------------------------------------------------ [ERROR] Fa ...
- C#使用CurrentUICulture切换语言
1. 创建2个窗口 2. 窗口1属性Localizable设置为True,Language选择英语(美国) 然后把窗口1中控件的Text由中文编辑成英文,Form2一样设置. 此时,Form1 ...