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 wordSample Output
ahat hatwordSponsor
题意:给出好多串,判断某个串能否由给出的串中的某两个串拼接而成。
思路:两个for循环。
利用map的find函数,以及string的截取功能。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<map>
using namespace std;
map<string,int>kk;
map<string,int>::iterator it;
int main()
{
string s;
while(cin>>s)
{
kk[s]=1;
}
for(it=kk.begin();it!=kk.end();it++)//遍历每个串
{
string a=it->first;// a为里面的参数
for(int j=1;j<a.size()-1;j++)//枚举分割点
{
string a1(a,0,j);//从0开始往后j位
string a2(a,j);
if(kk.find(a1)!=kk.end()&&kk.find(a2)!=kk.end())
{
cout<<a<<endl;
break;
}
}
}
return 0;
}
1252

被折叠的 条评论
为什么被折叠?



