hdu1247 Hat’s Words

http://acm.hdu.edu.cn/showproblem.php?pid=1247

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

struct tire
{
    bool isword;
    tire *next[26];
};
tire *head;
string s[50010];

bool cmp(string a,string b)
{
    return a.size()<b.size();
}

void Insert(string s)
{
    int i,j;
    tire *t=head,*p;
    for(i=0;i<s.size();i++)
    {
        int id=s[i]-'a';
        if(t->next[id]==NULL)
        {
            p=new tire;
            for(j=0;j<26;j++)
            {
                p->next[j]=NULL;
            }
            p->isword=false;
            t->next[id]=p;
        }
        t=t->next[id];
        if(t->isword==NULL)t->isword=false;
        if(i==s.size()-1)t->isword=true;
    }
}
bool Find(string s)
{
    tire *T=head,*t;
    int i,j;
    for(i=0;i<s.size();i++)
    {
        int id=s[i]-'a';
        if(T->next[id]==NULL)return false;
        else 
        {
            T=T->next[id];
            if(T->isword==true)
            {
                t=head;
                for(j=i+1;j<s.size();j++)
                {
                    id=s[j]-'a';
                    if(t->next[id]==NULL)break;
                    else 
                    {
                        t=t->next[id];
                    }
                }
                if(j>=s.size())
                {
                    if(t->isword==true)return true;
                }
            }
        }
    }
    return false;
}
int main()
{
    int i=0,sum=0,j=0,n;
    string a;
    head=new tire;
    for(j=0;j<26;j++)
    {
        head->next[j]=NULL;
    }
    head->isword=false;
    while(cin>>a)
    {
        s[i]=a;
        i++;
    }
    n=i;
    for(i=0;i<n;i++)//先把输入的数据全部储存到字典树
    {
        Insert(s[i]);
    }
    for(i=0;i<n;i++)
    {
        if(Find(s[i])==true)cout<<s[i]<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值