hdu 2846 Repository trie

#include<bits/stdc++.h>
using namespace std;
struct trie
{
    int cnt,num;
    int id;
    trie *son,*bro;
};

trie *rt=new trie;
int tot;
void settrie(char *s,int j)
{
    trie *p=rt,*t,*t1;
    int n=strlen(s),i,c;
    for(i=0; i<n; i++)
    {
        c=s[i]-'a';
        while(p->num!=-1&&p->num!=c)
        {
            t=p;
            p=p->bro;
            if(p->num>c)
            {
                p=t;
                break;
            }
        }
        //if(i==0&&c==3) printf("%d\n",++tot);
        if(p->num==-1)
        {
            p->cnt=1;
            p->num=c;
            t= new trie;
            t->id=-1;
            t->num=-1;
            t->cnt=0;
            p->bro=t;

            t= new trie;
            t->num=-1;
            t->cnt=0;
            t->id=-1;
            p->son=t;

            p->id=j;
            p=p->son;
        }
        else if(p->num==c)
        {
            if(p->id!=j) p->cnt++;
            p->id=j;
            p=p->son;
        }
        else
        {
            t= new trie;
            t->num=c;
            t->cnt=1;
            t->id=-1;
            t1= new trie;
            t1->id=-1;
            t1->num=-1;
            t1->cnt=0;
            t->son=t1;
            t->bro=p->bro;
            p->bro=t;
            p=t;
            p->id=j;
            p=p->son;
        }
    }
}


int main()
{
    int ans,c,n,i,j,len;
    char s[50];
    trie *p,*t;
    while(~scanf("%d",&n))
    {
        rt=new trie;
        rt->num=-1;
        rt->cnt=0;
        rt->id=-1;
        tot=0;
        for(i=0; i<n; i++)
        {
            scanf("%s",s);
            len=strlen(s);
            for(j=0; j<len; j++)
                settrie(s+j,i);
        }
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            scanf("%s",s);
            len=strlen(s);
            p=rt;
            ans=0;
            for(j=0; j<len; j++)
            {
                c=s[j]-'a';
                while(p->num!=-1&&p->num!=c)
                {
                    t=p;
                    p=p->bro;
                    if(p->num>c)
                    {
                        p=t;
                        break;
                    }
                }
                if(p->num==c)
                {
                    ans=p->cnt;
                    p=p->son;
                }
                else break;
            }
            if(j!=len) ans=0;
            printf("%d\n",ans);
        }
    }
    return 0;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct trie
{
    int cnt,num;
    trie *next[26];
};

trie *rt=new trie;

void settrie(char *s,int num)
{
    trie *p=rt,*t;
    int n=strlen(s),i,c;
    for(i=0;i<n;i++)
    {
        c=s[i]-'a';
       if(p->next[c]==NULL)
       {
           t=new trie;
           for(int j=0;j<26;j++)
            {
                t->next[j]=NULL;
                t->cnt=0;
                t->num=-1;
            }
           p->next[c]=t;
       }
       p=p->next[c];
        if(p->num!=num) p->cnt++;
        p->num=num;
    }
}


int main()
{
    int ans,c,n,i,j,len;
    char s[50];
    trie *p;
    while(~scanf("%d",&n))
    {
        for(i=0;i<26;i++)
        {
            rt->next[i]=NULL;
            rt->cnt=0;
            rt->num=-1;
        }
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            len=strlen(s);
            for(j=0;j<len;j++)
                settrie(s+j,i);
        }
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            len=strlen(s);
            p=rt;
            ans=0;
            for(j=0;j<len;j++)
            {
                c=s[j]-'a';
                if(p->next[c]==NULL) break;
                p=p->next[c];
            }
            if(j==len) ans=p->cnt;
            printf("%d\n",ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值