是題目沒有說清楚,還是我E文不夠好?一開始以為組成一個單詞的兩個部分不能相同,后來刪除這個檢測之后AC了。
很簡單,枚舉每個單詞分成哪兩個部分,看著兩個單詞在不在字典中。
具體做法,使用STL中的set即可。
以下是我的代碼:
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
//*/
set<string> words_set;
string s;
while(cin>>s)
words_set.insert(s);
vector<string> ans;
for(set<string>::const_iterator i=words_set.begin();i!=words_set.end();i++)
{
string t(*i);
for(int pos=1;pos<t.size();pos++)
{
string part1(t.begin(),t.begin()+pos);
if(!words_set.count(part1))
continue;
string part2(t.begin()+pos,t.end());
if(!words_set.count(part2))
continue;
ans.push_back(t);
break;
}
}
sort(ans.begin(),ans.end());
for(vector<string>::const_iterator i=ans.begin();i!=ans.end();i++)
cout<<*i<<endl;
return 0;
}
posted on 2011-03-06 19:13
lee1r 閱讀(494)
評論(0) 編輯 收藏 引用 所屬分類:
題目分類:數據結構