hdu1247 Hat’s Words

来源:互联网 发布:ie无法加载java插件 编辑:程序博客网 时间:2024/05/21 11:15

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
原创粉丝点击