[kuangbin带你飞]专题十七 AC自动机 D Detect the Virus

https://vjudge.net/contest/70326#problem/D

题意:给出n个编码后的模板串,然后有M次询问,每次询问输入一个编码后的文本串,问在编码前,有多少个模板串在文本串中出现过。


所以本题的烦点在于还原的过程,由于解码后的字符的ASCII值是从0~255的,所以不能用strlen,得自己记录长度。

自己模拟太烦了,看了bin神的结题报告才懂可以写的这么简洁,思路是以4个原字母为单位 (因为需要6-位>8位 那么四个字母会最终转化为三个字母) 。

void change(unsigned char str[],int len)
{
    int t=0;
    for(int i=0;i<len;i+=4)
    {
        buf[t++]=((str[i]<<2)|(str[i+1]>>4));
        if(i+2 < len)
            buf[t++]=( (str[i+1]<<4)|(str[i+2]>>2) );
        if(i+3 < len)
            buf[t++]= ( (str[i+2]<<6)|str[i+3] );
    }
    tot=t;
}
Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

struct Trie
{
    int node[520*64][256],fail[520*64],num[520*64];
    int root,L;
    int newnode()
    {
        for(int i = 0;i < 256;i++) node[L][i] = -1;
        num[L] = -1;
        return L++;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(unsigned char buf[],int len,int id)
    {
        int now = root;
        for(int i = 0;i < len;i++)
        {
            if(node[now][buf[i]] == -1)
                node[now][buf[i]] = newnode();
            now = node[now][buf[i]];
        }
        num[now] = id;
    }
    void build()
    {
        queue<int>Q;
        fail[root] = root;
        for(int i = 0;i < 256;i++)
            if(node[root][i] == -1)
                node[root][i] = root;
            else
            {
                fail[node[root][i]]=root;
                Q.push(node[root][i]);
            }
        while(!Q.empty())
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0;i < 256;i++)
                if(node[now][i] == -1)
                    node[now][i] = node[fail[now]][i];
                else
                {
                    fail[node[now][i]] = node[fail[now]][i];
                    Q.push(node[now][i]);
                }
        }
    }
    bool used[520];
    int query(unsigned char buf[],int len,int n)
    {
        memset(used,false,sizeof(used));
        int now = root;
        for(int i = 0;i < len;i++)
        {
            now = node[now][buf[i]];
            int temp = now;
            while( temp!=root )
            {
                if(num[temp] != -1)
                    used[num[temp]]=true;
                temp = fail[temp];
            }
        }
        int res = 0;
        for(int i = 0;i < n;i++)
            if(used[i])
                res++;
        return res;
    }
};

unsigned char buf[2050];
int tot;
char str[4000];
unsigned char s[4000];
unsigned char Get(char ch)
{
    if( ch>='A'&&ch<='Z' )return ch-'A';
    if( ch>='a'&&ch<='z' )return ch-'a'+26;
    if( ch>='0'&&ch<='9' )return ch-'0'+52;
    if( ch=='+' )return 62;
    else return 63;
}
void change(unsigned char str[],int len)
{
    int t=0;
    for(int i=0;i<len;i+=4)
    {
        buf[t++]=((str[i]<<2)|(str[i+1]>>4));
        if(i+2 < len)
            buf[t++]=( (str[i+1]<<4)|(str[i+2]>>2) );
        if(i+3 < len)
            buf[t++]= ( (str[i+2]<<6)|str[i+3] );
    }
    tot=t;
}
Trie ac;
int main()
{
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
    int n,m;
    while(scanf("%d",&n) == 1)
    {
        ac.init();
        for(int i = 0;i < n;i++)
        {
            scanf("%s",str);
            int len = strlen(str);
            while(str[len-1]=='=')len--;
            for(int j = 0;j < len;j++)
            {
                s[j] = Get(str[j]);
            }
            change(s,len);
            ac.insert(buf,tot,i);
        }
        ac.build();
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",str);
            int len=strlen(str);
            while(str[len-1]=='=')len--;
            for(int j = 0;j < len;j++)
                s[j] = Get(str[j]);
            change(s,len);
            printf("%d\n",ac.query(buf,tot,n));
        }
        printf("\n");
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值