uva 10391 Compound Words <set>

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 theconcatenation 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, inalphabetical order.

Sample Input  

a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra

Sample Output  

alien
newborn


用set做
#include <iostream>
#include <set>
#include <string>
using namespace std;

set<string>dic;
set<string>::iterator t;

int main()
{
    string word;
    while(cin >> word)dic.insert(word);
    
    for(t = dic.begin(); t != dic.end(); t++){
        word = *t;
        int n = word.length();

        for(int i = 0; i < n - 1; i++ ){
            string s1=word.substr(0,i+1),s2 = word.substr(i+1,n-i-1);
            if( (dic.count(s1)) && (dic.count(s2)) ){
                cout << word <<endl;
                break;
            }
        }
    }
    //system("pause");
    return 0;
}

 

 

dic.count(s2)  在set dic中计算s2的个数,并返回其个数。

 

posted @ 2016-05-10 12:58  Not-Bad  阅读(211)  评论(0编辑  收藏  举报