复合词(Compound Words, UVa 10391)(stl set)

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<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<set>
#include<map>
#include<string>
using namespace std;
typedef long long LL;
int main()
{
    string str;
    set<string> Set;
    while(cin>>str)
    {
        Set.insert(str);
    }
    set<string>::iterator it;
    for(it=Set.begin();it!=Set.end();it++)
    {
        string str1=*it;
        int l=str1.size();
        for(int i=0;i<l;i++)
        {
            string one=str1.substr(0,i+1);
            string two=str1.substr(i+1,l-i);
            if(Set.find(one)!=Set.end()&&Set.find(two)!=Set.end())
            {
                cout<<str1<<endl;
                break;
            }
        }
    }
    return 0;
}

代码:

原文地址:https://www.cnblogs.com/yinbiao/p/9354169.html