您的位置:首页 > 其它

uva 10391 compound words

2011-05-22 14:38 363 查看
保存好单词,枚举每个单词的子串即可。

#include <set>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <cstdlib>
using namespace std;
#undef _DEBUG
int main() {
set <string> dict;
char word[1024];
//  while(scanf("%s", word) == 1) {
while(gets(word) && *word) {
dict.insert(word);
#ifdef _DEBUG
printf("word = %s/n", word);
#endif
char *p = word;
while(*p) {
assert('a' <= *p && *p <= 'z');
++p;
}
}
for(set <string>::iterator it = dict.begin(); it != dict.end(); ++it) {
const string &str = *it;
for(int i=1; i<str.size(); ++i) {
string a = str.substr(0, i);
string b = str.substr(i, str.size() - i);
#ifdef _DEBUG
printf("a = %s, %b = %s/n", a.c_str(), b.c_str());
#endif
if(/*a != b && */dict.find(a) != dict.end() && dict.find(b) != dict.end()) {
printf("%s/n", str.c_str());
break;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: