hdu1247 Hat’s Words

来源:互联网 发布:大音希声大象无形 知乎 编辑:程序博客网 时间:2024/06/05 15:23

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
aahathathatwordhzieeword
 

Sample Output
ahathatword
 

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];n++)q.insert(s[n]);  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;}


0 0
原创粉丝点击