hdu-1247 Hat’s Words

//主要思想是把每一个单词拆开,然后找在树中查找是否存在该单词。
#include<cstdio>
#include<cstring>
#include<malloc.h>
typedef struct node
{
    bool flag;
    struct node *next[26];
}*tree;

void insert(tree h,char *s)
{
    tree p=h;
    int len=strlen(s),i;
    for(i=0;i<len;i++)
    {
        int index=s[i]-'a';
        if(p->next[index]!=NULL)
        {
            p=p->next[index];
        }
        else
        {
            tree tem=(tree)calloc(1,sizeof(node));
            tem->flag=0;
            p->next[index]=tem;
            p=tem;
        }
    }
    p->flag=1;
}
bool find(tree h,char *s,int x,int y)
{
    tree p=h;
    //int len=strlen(s),
    int i;
    for(i=x;i<y;i++)
    {
        int index=s[i]-'a';
        if(p->next[index]!=NULL)
            p=p->next[index];
        else return 0;
    }
    return p->flag;
}
void delet(tree head)
{
    int i;
    for(i=0;i<26;i++)
    {
        if(head->next[i]!=NULL)
            delet(head->next[i]);
    }
    free(head);
}
char s[50001][30];
int main()
{
    tree head=(tree)calloc(1,sizeof(node));
    head->flag=0;
    int j,k,i=0;
    while(scanf("%s",s[i])!=EOF)
    {
        insert(head,s[i++]);
    }
    for(j=0;j<i;j++)
    {
        int len=strlen(s[j]);
        for(k=1;k<len-1;k++)
        {
            if(find(head,s[j],0,k)&&find(head,s[j],k,len))
            {
                printf("%s\n",s[j]);
                break;
            }
        }
    }
    delet(head);
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值