UVa 10391 (水题 STL) Compound Words

今天下午略感无聊啊,切点水题打发打发时间,=_=||

把所有字符串插入到一个set中去,然后对于每个字符串S,枚举所有可能的拆分组合S = A + B,看看A和B是否都在set中,是的话说明S就是一个复合词。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <string>
 6 #include <set>
 7 #include <vector>
 8 using namespace std;
 9 
10 set<string> dic;
11 vector<string> ans;
12 set<string>::iterator it;
13 
14 int main()
15 {
16     //freopen("in.txt", "r", stdin);
17 
18     string s;
19     while(cin >> s) dic.insert(s);
20     for(it = dic.begin(); it != dic.end(); it++)
21     {
22         s = *it; int l = s.length();
23         for(int i = 1; i < l; i++)
24         {
25             string s1 = s.substr(0, i);
26             string s2 = s.substr(i, l - i);
27             if(dic.count(s1) && dic.count(s2))
28             {
29                 ans.push_back(s);
30                 break;
31             }
32         }
33     }
34 
35     sort(ans.begin(), ans.end());
36     for(int i = 0; i < ans.size(); i++) cout << ans[i] << endl;
37 
38     return 0;
39 }
代码君

 

posted @ 2015-04-24 15:53  AOQNRMGYXLMV  阅读(259)  评论(0编辑  收藏  举报