您的位置:首页 > 其它

【处理空格】poj2503——Babelfish

2013-08-10 10:31 316 查看
题目:点击打开链接

很水的一道题,但是处理两行之间的空格花了许多功夫,最后还是用了char,因为可以自由修改,相比string需要用insert函数,更加灵活一些。也可以用hash来做,不过有些麻烦就是了。

#include <iostream>
#include <string>
#include <map>
#include <cstdio>
using namespace std;

int main()
{
	char d1[50],d2[50];
	string tp;
	map<string,string> dic;
	int count=0;
	while(1)
	{
		char t=getchar(); 
		if(t=='\n')  
			break;
		else     
		{
			d1[0]=t;
			int i=1;
			while(1)
			{
				t=getchar();
				if(t==' ')
				{
					d1[i]='\0';
					break;
				}
				else
					d1[i++]=t;
			}
		}
		
		cin>>d2;
		getchar();  //吃掉 输入foreign后的 回车符
		dic[d2]=d1;
		count++;
	}
	string tar;
	while(cin>>tar)
	{
		map<string,string>::iterator p=dic.find(tar);
		if(p==dic.end())
			cout<<"eh"<<endl;
		else
			cout<<(*p).second<<endl;
	}
	
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: