码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 10391 Compound Words(复合词)

时间:2015-01-21 18:13:27      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:acm   紫书   

题意  输出所有输入单词中可以由另两个单词的组成的词

STL set的应用  枚举每个单词的所有可能拆分情况  看拆开的两个单词是否都存在  都存在的就可以输出了

#include <bits/stdc++.h>
using namespace std;
string a, b;
set<string> s;
set<string>::iterator i;

int main()
{
    int l;
    while(cin >> a) s.insert(a);
    for(i = s.begin(); i != s.end(); ++i)
    {
        l = i->length();
        for(int j = 1; j < l - 1; ++j)
        {
            a = i->substr(0, j);
            b = i->substr(j);
            if(s.count(a) && s.count(b))
            {
                cout << (*i) << endl;
                break;
            }
        }
    }
    return 0;
}

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


UVa 10391 Compound Words(复合词)

标签:acm   紫书   

原文地址:http://blog.csdn.net/acvay/article/details/42971329

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!