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

把所有字符串插入到一个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 }
代码君

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2021-12-18
  • 2021-05-21
  • 2022-12-23
  • 2021-12-26
猜你喜欢
  • 2021-08-18
  • 2021-12-27
  • 2022-12-23
  • 2021-07-29
  • 2021-12-29
  • 2021-11-23
相关资源
相似解决方案