hdu 1075 What Are You Talking About

乱写的代码。之所以乱写,是为了复习字典树和map

要注意文章单词间可能有多个空格和字符

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
using namespace std;
map<string,string>mapp;
string str;
struct stu
{
	int flag;
	stu* a[26];
	stu()
	{
		flag=0;
		for(int i=0;i<26;i++) a[i]=NULL;
	}
};
stu* root=new stu();
void build(stu *root,int cnt)
{
	int x=str[cnt]-'a';
	if(root->a[x]==NULL) root->a[x]=new stu();
	root=root->a[x];
	if(cnt==str.size()-1)
	{
		root->flag=1;
		return;
	}
	build(root,cnt+1);
}
string find(stu* root,int cnt)
{
	int x=str[cnt]-'a';
	if(x<0||x>25) return str;
	if(root->a[x]==NULL) return str;
	root=root->a[x];
	if(cnt==str.size()-1)
	{
		if(root->flag==1) return mapp[str];
		else return str;
	}
	else return find(root,cnt+1);
}
int main()
{
	//cin.sync_with_stdio(false);
	string cmd;
	cin>>cmd;
	while(cin>>cmd&&cmd!="END")
	{
		cin>>str;
		mapp[str]=cmd;
		build(root,0);
	}
	cin>>cmd;
	getchar();
	while(getline(cin,cmd)&&cmd!="END") 
	{
		for(int i=0;i<cmd.size();i++)
		{
			str="";
			while(cmd[i]>='a'&&cmd[i]<='z') str+=cmd[i++];
			cout<<find(root,0)<<cmd[i];
		}
		cout<<endl;
	}
	return 0;
} 


posted on 2017-07-23 14:24  wgwyanfs  阅读(150)  评论(0编辑  收藏  举报

导航