首页 > 代码库 > uva10391(复合词)

uva10391(复合词)

第一次做是用暴力的方法一个一个找,意料之中超市了;

然后就想把每个单词拆分,虽然时间复杂度也是平方级别,但是单词不会太长,循环次数小很多,试过之后果然AC了!

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cstdio>
 6 #include <set>
 7 #include <string>
 8 #include <sstream>
 9 #include <vector>
10 
11 using namespace std;
12 set<string> words;
13 set<string> answers;
14 int main()
15 {
16     string s,buf;
17     int i;
18     while(cin >> s)
19         words.insert(s);
20     set<string>::iterator it;
21     for(it = words.begin();it != words.end();it++)
22     {
23         s = *it;
24         for(i=0;i<s.length()-1;i++)
25         {
26             string a = s.substr(0,i+1);
27             string b = s.substr(i+1,s.length()-i-1);
28             if(words.find(a)!=words.end() && words.find(b)!=words.end())
29                 answers.insert(s);
30         }
31     }
32     for(it = answers.begin();it!= answers.end();it++)
33         cout << *it << "\n";
34     return 0;
35 }

 

uva10391(复合词)