Hat’s Words

A - Hat’s Words

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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


通过map存单词,在遍历每个单词,枚举单词的拆分,通过map的find函数来查找拆分出的两个单词,查找的时间复杂度为O(logn)


#include<iostream>
#include<stdlib.h>
#include<string>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;
map<string,int> mp;
map<string,int>::iterator it;
int main()
{
	ios::sync_with_stdio(false);
	int sum=0;
	string a;
	while(cin>>a)
	{
		mp[a]=1;
	}
	for(it=mp.begin();it!=mp.end();it++)
	{
		a=it->first;
		for(int i=0;i<a.size()-1;i++)
		{
			string a1(a,0,i);
			string a2(a,i);
			if(mp.find(a1)!=mp.end()&&mp.find(a2)!=mp.end())
			{
				cout<<a<<endl;
				break;
			}
		}
	}		
}




猜你喜欢

转载自blog.csdn.net/qq_38570571/article/details/81034970