HDU 1113 Word Amalgamation

这题和刘汝佳的《入门经典》P79,十分相似,基本一样的吧

刘汝佳用了qsort,C语言的内容,还不错。这里用C++,STL的方法

1.如果用直接模拟的方法做,比较麻烦

2.map的方法可以作为索引的方法,很快可以访问到相对的字符串

3.索引的建造方法:每个字符串都排序,作为每个字符串的值,对应的键可以对应这个值,每次查找字典即可。 

 1 #include <stdio.h>
 2 #include <map>
 3 using namespace std;
 4 #include <string.h>
 5 #include <string>
 6 #include <iostream>
 7 #include <algorithm>
 8 
 9 multimap<string,string> mp;
10 multimap<string,string>::iterator it;
11 
12 int main()
13 {
14     char st[130];
15     while(~scanf("%s",st))
16     {
17         mp.clear();
18         string str = string(st);
19         sort(st,st+strlen(st));
20         mp.insert(make_pair(str,string(st)));
21         while(scanf("%s",st) && strcmp(st,"XXXXXX")!=0)
22         {
23             str = string(st);
24             sort(st,st+strlen(st));
25             mp.insert(make_pair(str,string(st)));
26         }
27         while(scanf("%s",st))
28         {
29             if(strcmp(st,"XXXXXX")==0) return 0;
30             int cnt = 0;
31             sort(st,st+strlen(st));
32             string txt = string(st);
33             for(it=mp.begin();it!=mp.end();it++)
34             {
35                 if(it->second == txt) 
36                 {
37                     cnt++;
38                     cout<<it->first<<endl;
39                 }
40             }
41             if(cnt==0) cout<<"NOT A VALID WORD
";
42             cout<<"******"<<endl;
43         }
44     }
45     return 0;
46 }
原文地址:https://www.cnblogs.com/cton/p/3435621.html