24h購物| | PChome| 登入
2012-06-08 14:13:07| 人氣1,965| 回應0 | 上一篇 | 下一篇

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

推薦 0 收藏 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;
}
 

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

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文