题目连接在这里:http://acm.hdu.edu.cn/showproblem.php?pid=1075。
看了网上题解,几乎都用trie树。我用用map映照容器过了3000+ms,有点慢~,首先说map容器,自我感觉map容器很神奇,将你想要的两个数据类型捆绑在一起方便了很多的处理。当map容器中映照数据为字符串时,建议定义为c++的string类型,因为char数组不好直接赋值,同时要熟练掌握string的使用方法(与char数组不一样)。
这里首先贴下STL map版本的,等字典树学会了在贴trie树版的!
#include<cstdio> #include<iostream> #include<string> #include<map> #include<cstring> #include<algorithm> using namespace std; #define read freopen("zx.in","r",stdin) #define write freopen("zx.out","w",stdout) int main() { read, write; map<string,string>arr; string buf1,buf2,buf; char s[10],temp[3010]; scanf("%s",s); while(cin>>buf1 && buf1!="END") { cin>>buf2; arr[buf2]=buf1; } scanf("%s/n",s);//同时吸收了回车符 buf="";//从空串开始往里面加入元素 while(1) { gets(temp); if(strcmp(temp,"END")==0) break; int len=strlen(temp); for(int i=0;i<len;i++) { if(!(temp[i]>='a' && temp[i]<='z')) { if(arr[buf]!="") cout<<arr[buf]; else cout<<buf; buf=""; printf("%c",temp[i]); } else buf+=temp[i]; } printf("/n"); } return 0; }