島嶼渡假↘下殺$4900起女大生禁不起誘惑!按了..攝影界美人正妹指定相機款老伯送飲料 遭學生糾察...
2012-06-08 14:13:07 人氣(62) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

[UVA][set使用] 10391 - Compound Words

0
收藏
0
推薦

Problem E: Compound Words

You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.

Input

Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 120,000 words.

Output

Your output should contain all the compound words, one per line, in alphabetical order.

Sample Input

a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra

Sample Output

alien
newborn



#include <stdio.h>
#include <iostream>
#include <set>
using namespace std;
int main() {
string line, s1, s2;
set<string> S;
while(cin >> line)
S.insert(line);
for(set<string>::iterator i = S.begin(); i != S.end(); i++) {
int len = (*i).length();
for(int j = 0; j < len-1; j++) {
s1 = (*i).substr(0, j+1);
s2 = (*i).substr(j+1, len-j);
if(S.find(s1) != S.end() && S.find(s2) != S.end()) {
cout << (*i) << endl;
break;
}
}
}
return 0;
}
 

10391Compound Wordsset
台長:Morris
人氣(62) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 320 - Border
此分類上一篇:[UVA][path] 452 - Project Scheduling

我要回應
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入以下數字 (ex:123)

(有*為必填)
詳全文