hdu 1247

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

题意:给你一些单词,让你判断每一单词能否再给你的这些单词中找到两个不同的单词组成,如果能就输出;

理解题意后,就剩怎样查询了,在查询中我用了两成for循环解决了问题;

代码如下:

#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cstring>
#define Max 28
using namespace std;
struct dot
{
	dot *next[Max];
	int flag;
};
dot root;
dot *newnode()
{
	dot *temp=new dot;
	temp->flag=0;
	for(int i=0;i<Max;i++)
	   temp->next[i]=NULL;
    return temp;
}
void creatree(char *st)
{
	int len=strlen(st);
	int id=0;
	dot *p=&root;
	for(int i=0;i<len;i++)
	{
		id=st[i]-'a';
        if(p->next[id]==NULL)
            p->next[id]=newnode();
        p=p->next[id];
	}
    p->flag=1;
}
int find(char *st)
{
	int len=strlen(st);
	int id=0;
	dot *p=&root;
	for(int i=0;i<len-1;i++)
	{
		id=st[i]-'a';
        if(p->next[id]==NULL)
           return 0; 
        p=p->next[id]; 
		if(p->flag)
		   {     dot *q=&root;
		         int k=1;
		         int im=0;
			   for(int j=i+1;j<len;j++)
			   {
				  im=st[j]-'a';
				  if(q->next[im]==NULL)
				     { k=0;
					   break;
				     }
                  q=q->next[im];
			   }
			   if(k&&q->flag)
			   	  return 1;
	       
		   } 
	}
	return 0;
}	
char st[50005][20];
int main()
{

	int i,j,k;
	while(scanf("%s",&st[i])!=EOF)
		creatree(st[i++]);
	for(j=0;j<i;j++)
	{   
		k=find(st[j]);
		if(k) cout<<st[j]<<endl;
	}
	return 0;
}


转载于:https://www.cnblogs.com/wangyumin/p/5323471.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值