HDU-3695-ac自动机

char str1[5100009], str2[5100009];
int cnt;

struct trie
{
    trie *ch[26];
    trie *fail;
    int end;
    void init()
    {
        memset(ch, NULL, sizeof(ch));
        fail = NULL;
        end = 0;
    }
}*root, t[maxn];

void insert(char *s)
{
    trie *p = root;
    for(int i=0; s[i]; i++)
    {
        //cout<<i<<' '<<s[i]<<endl;
        int k = s[i]-'A';
        if(p->ch[k]==NULL)
        {
            t[cnt].init();
            p->ch[k] = &t[cnt++];
        }
        p = p->ch[k];
    }
    p->end = 1;
}

queue<trie*>q;
void build_fail()
{
    q.push(root);
    root->fail = root;
    while(!q.empty())
    {
        trie *p = q.front();
        q.pop();
        for(int k=0; k<26; k++)
        if(p->ch[k]!=NULL)
        {
            q.push(p->ch[k]);
            if(p==root)
            {
                p->ch[k]->fail = root;
                continue;
            }
            trie *tmp = p->fail;
            while( tmp!=root && tmp->ch[k]==NULL)
                tmp = tmp->fail;
            if(tmp->ch[k]) p->ch[k]->fail = tmp->ch[k];
            else p->ch[k]->fail = root;
        }
    }
}

int query(char *a)
{
    int ans = 0;
    trie *p = root;
    for(int i=0; a[i]; i++)
    {
        if(a[i]<'A' || a[i]>'Z')
        {
            p = root;
            continue;
        }
        int k = a[i]-'A';
        while(p->ch[k]==NULL && p!=root) p=p->fail;
        p = p->ch[k];
        p = (p==NULL)? root : p;
        trie *tmp = p;
        while(tmp!=root && tmp->end!=0)
        {
            ans++;
            tmp->end = 0;
            tmp = tmp->fail;
        }
    }
    return ans;
}

int main()
{
    int tt, n, i, j;
    scanf("%d", &tt);
    while(tt--)
    {
        cnt = 1;
        t[0].init();
        root = &t[0];
        scanf("%d", &n);
        while(n--)
        {
            scanf("%s", str1+1);
            for(i=1, j=1; str1[i]; i++)
            {
                if(str1[i]>='A' && str1[i]<='Z') str2[j++] = str1[i];
                else
                {
                    i++;
                    int k = str1[i]-'0';
                    i++;
                    while(str1[i]>='0'&&str1[i]<='9') k = k*10+ str1[i++]-'0';
                    while(k--) str2[j++] = str1[i];
                    i++;
                }
            }
            str2[j] = '\0';
            insert(str2+1);
        }
        build_fail();

        scanf("%s", str1+1);
        for(i=1, j=1; str1[i]; i++)
        {
            if(str1[i]>='A' && str1[i]<='Z') str2[j++] = str1[i];
            else
            {
                i++;
                int k = str1[i]-'0';
                i++;
                while(str1[i]>='0'&&str1[i]<='9') k = k*10+ str1[i++]-'0';
                while(k--) str2[j++] = str1[i];
                i++;
            }
        }
        str2[j] = '\0';
            //cout<<str2+1<<endl;
        int ans = query(str2+1);
        for(i=1, j=strlen(str2+1); i<j; i++, j--)
            swap(str2[i], str2[j]);
        ans += query( str2+1 );
        printf("%d\n", ans);
    }
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值