这个比赛我是4月7号才报名,本身有点水,加上时间仓促杂事一堆,所以没做多少。
传话游戏题目描述如下:
————————————————————————————————————————————————————
时间限制: 1000ms 内存限制: 256MB
Alice和Bob还有其他几位好朋友在一起玩传话游戏。这个游戏是这样进行的:首先,所有游戏者按顺序站成一排,Alice站第一位,Bob站最后一位。然后,Alice想一句话悄悄告诉第二位游戏者,第二位游戏者又悄悄地告诉第三位,第三位又告诉第四位……以此类推,直到倒数第二位告诉Bob。两位游戏者在传话中,不能让其他人听到,也不能使用肢体动作来解释。最后,Bob把他所听到的话告诉大家,Alice也把她原本所想的话告诉大家。
由于传话过程中可能出现一些偏差,游戏者越多,Bob最后听到的话就与Alice所想的越不同。Bob听到的话往往会变成一些很搞笑的东西,所以大家玩得乐此不疲。经过几轮游戏后,Alice注意到在两人传话中,有些词汇往往会错误地变成其他特定的词汇。Alice已经收集到了这样的一个词汇转化的列表,她想知道她的话传到Bob时会变成什么样子,请你写个程序来帮助她。
输入包括多组数据。第一行是整数 T,表示有多少组测试数据。每组数据第一行包括两个整数 N 和 M,分别表示游戏者的数量和单词转化列表长度。随后有 M 行,每行包含两个用空格隔开的单词 a 和 b,表示单词 a 在传话中一定会变成 b。输入数据保证没有重复的 a。最后一行包含若干个用单个空格隔开的单词,表示Alice所想的句子,句子总长不超过100个字符。所有单词都只包含小写字母,并且长度不超过20,同一个单词的不同时态被认为是不同的单词。你可以假定不在列表中的单词永远不会变化。
对于每组测试数据,单独输出一行“Case #c: s”。其中,c 为测试数据编号,s 为Bob所听到的句子。s 的格式与输入数据中Alice所想的句子格式相同。
1 ≤ T ≤ 100
小数据:2 ≤ N ≤ 10, 0 ≤ M ≤ 10
大数据:2 ≤ N ≤ 100, 0 ≤ M ≤ 100
2 4 3 ship sheep sinking thinking thinking sinking the ship is sinking 10 5 tidy tiny tiger liar tired tire tire bear liar bear a tidy tiger is tired
Case #1: the sheep is thinking Case #2: a tiny bear is bear
#include<iostream> #include<string> #include<stdio.h> using namespace std; int n,m; string s; string ss; struct node { string s1; string s2; int next; }nod[200]; void check(string tmp) { for(int i = 0;i<m;++i) { if(tmp == nod[i].s1) { int ind = i; int cntdown = n-2; while(cntdown) { cntdown--; if(nod[ind].next!=-1) ind = nod[ind].next; } ss.append(nod[ind].s2); ss.append(" "); return; } } ss.append(tmp+" "); } int main() { int tc; char c; int cnt = 0; cin>>tc; while(tc--) { cin>>n>>m; if(m == 0) { scanf("%c",&c); getline(cin,s); cout<<"Case #"<<++cnt<<": "<<s<<endl; continue; } for(int i=0;i<m;++i) { cin>>nod[i].s1>>nod[i].s2; nod[i].next = -1; } for(int i=0;i<m;++i) { for(int j = 0;j<m;++j) { if(nod[i].s2 == nod[j].s1) { nod[i].next = j; break; } } } scanf("%c",&c); getline(cin, s); ss.clear(); for(int i=0;i<s.length();++i) { int l = i+1; while(l<s.length() && s[l]!=' '){l++;} string tmp = ""; for(int j=i;j<l;++j) { tmp.append(1,s[j]); } check(tmp); i=l; } cout<<"Case #"<<++cnt<<": "; for(int i =0;i<ss.length()-1;++i) cout<<ss[i]; cout<<endl; } }
#include<stdio.h> #include<cstring> using namespace std; int n,m; struct node { char s1[50]; char s2[50]; int next; }nod[105]; char s[200]; char ss[200]; int ss_i; void check(char tmp[50]) { if(tmp[0]=='\0' || tmp==NULL) { while(true){}; } for(int i = 0;i<m;++i) { if(strcmp(tmp,nod[i].s1)==0) { int ind = i; int cntdown = n-2; while(cntdown) { cntdown--; if(nod[ind].next!=-1) ind = nod[ind].next; } for(int j=0;j<strlen(nod[ind].s2);++j) ss[ss_i++] = nod[ind].s2[j]; ss[ss_i++] = ' '; return; } } for(int i = 0;i<strlen(tmp);++i) ss[ss_i++] = tmp[i]; ss[ss_i++] = ' '; } int main() { char c_b; int tc; int cnt = 0; scanf("%d",&tc); while(tc--) { ss_i = 0; scanf("%d%d",&n,&m); if(m == 0) { scanf("%c",&c_b); scanf("%[^\n]", s); scanf("%c",&c_b); printf("Case #%d: %s\n",++cnt ,s); continue; } for(int i=0;i<m;++i) { scanf("%s", &nod[i].s1); scanf("%s", &nod[i].s2); nod[i].next = -1; } for(int i = 0;i<m;++i) { for(int j=0;j<m;++j) { if(strcmp(nod[j].s1, nod[i].s2)==0) { nod[i].next = j; break; } } } scanf("%c",&c_b); scanf("%[^\n]", s); scanf("%c",&c_b); for(int i=0;i<strlen(s);++i) { int l = i+1; while(l<strlen(s) && s[l]!=' '){l++;} char tmp[50]; for(int j = i;j<l;++j) tmp[j-i] = s[j]; tmp[l-i] = '\0'; check(tmp); i=l; } ss[ss_i-1] = '\0'; printf("Case #%d: %s\n",++cnt ,ss); } }
//第一题来自 mochavic #include <stdio.h> #include <string> #include <map> using namespace std; map<string, string> mp, mpp; char c[110][30]; char str[110]; string s; int main(){ int T, n, m, i, j, ri = 1; scanf("%d", &T); while (T--){ mp.clear(); mpp.clear(); scanf("%d%d", &n, &m); for (i = 0; i < m; i++){ scanf("%s%s", c[i], str); mp[c[i]] = str; } for (i = 0; i < m; i++){ s = c[i]; for (j = 1; j < n; j++){ if (mp.count(s) == 0) break; s = mp[s]; } mpp[c[i]] = s; } gets(str); gets(str); printf("Case #%d: ", ri++); s = ""; for (i = 0; str[i]; i++){ if (str[i] >= 'a' && str[i] <= 'z') s += str[i]; else{ printf("%s%c", (mpp.count(s)==0)?s.c_str():mpp[s].c_str(), str[i]); s = ""; } } if (s != "") printf("%s", (mpp.count(s)==0)?s.c_str():mpp[s].c_str()); printf("\n"); } return 0; }