2013编程之美 资格赛 传话游戏

这个比赛我是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
————————————————————————————————————————————————————————————

      这题我使用的是最笨的方法,用一个类node里面存着两个字符串s1 s2,一个是原单词,另外一个是曲解单词,再存一个next,代表被曲解的单词可能被再次曲解时对应的node编号。
读入被曲解的单词列表,接着用循环得到每一个node里面的next值是多少。
最后读入句子,提取句子里面的每个单词,逐一从node里面找有没有曲解的可能,一个单词被曲解后可能继续可以被曲解,这个时候不需要重新扫描node,只需要直接查next就可以知道其被曲解的下一个结果。
这里复杂度是O(m*m)+O(n),最欢情况是100*100*100=1 000 000,应该不会超

代码中的注意事项:
      这里我用了getline函数,这个函数的使用我也有点头痛,有时候会把之前输入里面的换行符“\n”读入,导致直接结束输入,所以在使用getline前如果有cin之类的输入操作,可以先检查是否出现输入错误,如果有加个scanf("%c",&c),c是char类型变量,这样将换行符读取后,即可正常使用.

代码:
写了两个版本的代码
1 第一个版本用的是char*的方法,不知为什么小数据会runtime error,检查了很久也不知道,大数据就AC了,如果哪位高手知道错在哪里,请指出,不胜感激。
2 第二个版本的代码是用C++ string,这个是小数据和大数据都AC的。
先贴出第二个版本的,小数据大数据都通过了,如下:
#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;
}



你可能感兴趣的:(2013编程之美 资格赛 传话游戏)