复合词(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;
}
复制代码

 

代码:

 

posted @   西*风  阅读(568)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示