Hat’s Words
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 5
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.
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
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<string,int>mp;
string s[120010];
int i=0;
while(cin>>s[i])
mp[s[i++]]=1;
int n=i;
for(int i=0;i<n;i++)
{
for(int j=0;j<s[i].size();j++)
{
string ss=s[i].substr(0,j);//从下标为0开始截取长度为j位
string s1=s[i].substr(j);//只有一个数字j表示从下标为j开始一直到结尾
if(mp[ss]&&mp[s1])
{
cout<<s[i]<<endl;
break;
}
}
}
return 0;
}