您的位置:首页 > 其它

hdu 1075 What Are You Talking About

2015-08-23 14:05 274 查看
乱写的代码,之所以乱写,是为了复习字典树和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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: