您的位置:首页 > 其它

hdu1247 Hat’s Words

2016-02-04 16:58 344 查看


Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11867    Accepted Submission(s): 4225

Problem Description

A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.

You are to find all the hat’s words in a dictionary.

 

Input

Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.

Only one case.

 

Output

Your output should contain all the hat’s words, one per line, in alphabetical order.

 

Sample Input

a
ahat
hat
hatword
hziee
word

 

Sample Output

ahat
hatword

 

Author

戴帽子的

 

Recommend

Ignatius.L   |   We have carefully selected several similar problems for you:  1298 1800 2846 2222 1711 

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<set>
using namespace std;

const int maxn=5e4;
string s[maxn+5];
set<string> q;

int main()
{
//freopen("1.in","r",stdin);

string s1,s2;
int n,i,j,k;
for(n=0;cin>>s
;n++)q.insert(s
);
for(i=0;i<n;i++)
for(s1="",k=s[i].length(),j=0;j<k;j++)
{
s1+=s[i][j],s2=s[i].substr(j+1);
if(q.find(s1)!=q.end() && q.find(s2)!=q.end())
{
cout<<s[i]<<endl;
break;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: