【ZOJ 3430 Detect the Virus】 AC自动机

ZOJ3430
题意就是给你一个加密的单词,加密方式就是先把字符按照ASCII表转为2进制,再截取6个为一段转为10进制,再去表中改为对应字符。
我们只需要把所有关键字和文本串都解码之后,就是一个最朴素的AC自动机了。
密文转换方法是向kuangbin大神学习的,我们通过思考可以发现,解码的过程就是把四个字符转换为3个字符的过程,所以我们只要对加密串进行一些二进制操作,就可以直接将24位二进制码分为3个八位二进制码。具体过程请参考代码。
注意此题字符可能有256种,所以要用unsigned char
而且 ZOJ RE和MLE均返回 Segmentation Fault ,此题卡内存比较严重,需要好好计算一下
ZOJ3430代码

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
#include<set>
using namespace std;
const int maxn =5e4+5;
unsigned char aa[maxn];
#define dbg(x) cout<<#x<<" "<<x<<endl;
struct ACTrie
{
  int tree[maxn][256], fail[maxn],end_[maxn];
  int root,cnt,num;
  set<int> s;
  int newnode()
  {
      for(int i=0;i<256;i++)
        tree[cnt][i]=-1;
      end_[cnt]=0;
      return cnt++;
  }
  void init()
  {
      cnt=0;
      num=0;
      root=newnode();
  }
  void insert_(unsigned char str[],int len)
  {
      int pos=root;
      for(int i=0;i<len;i++)
      {
          int id=str[i];
          if(tree[pos][id]==-1) tree[pos][id]=newnode();
          pos=tree[pos][id];
      }
      end_[pos]=++num;
  }
  void build()
  {
      queue<int> que;
      fail[root]=root;
      for(int i=0;i<256;i++)
      {
          if(tree[root][i]==-1) tree[root][i]=root;
          else
          {
              fail[tree[root][i]]=root;
              que.push(tree[root][i]);
          }
      }
      while(!que.empty())
      {
          int now=que.front();
          que.pop();
          for(int i=0;i<256;i++)
          {
              if(tree[now][i]==-1) tree[now][i]=tree[fail[now]][i];
              else
              {
                  fail[tree[now][i]]=tree[fail[now]][i];
                  que.push(tree[now][i]);
              }
          }
      }
  }
  int query(unsigned char *str,int len)
  {
       s.clear();
        int now=root;
        int res=0;
        for(int i=0;i<len;i++)
        {
            int id=(int)str[i];
            now=tree[now][id];
            int temp=now;
            while(temp!=root)
            {
                if(end_[temp]!=0) s.insert(end_[temp]);
                temp=fail[temp];
            }
        }
        return (int)s.size();
  }
};
unsigned char cal(char c)
{
    if(c>='A'&&c<='Z')
    {
        return c-'A';
    }
    if(c>='a'&&c<='z')
    {
        return c-'a'+26;
    }
    if(c>='0'&&c<='9')
    {
        return c-'0'+52;
    }
    if(c=='+')  return 62;
    else return 63;
}
int change(unsigned char str[],int len)
{
    int t=0;
    for(int i=0;i<len;i+=4)//每四个字符计算一次,转换为三个字符
    {
        aa[t++]=((str[i]<<2)|(str[i+1]>>4));//截取第一个字符的全部+第二个字符前两位
        if(i+2 < len)
            aa[t++]=( (str[i+1]<<4)|(str[i+2]>>2) );//截取第二个字符后四位+第三个字符前四位
        if(i+3 < len)
            aa[t++]= ( (str[i+2]<<6)|str[i+3] );//截取第三个字符后两位+第四个字符全部
    }
    return t;
}
ACTrie ac;
char str[maxn];
unsigned char ss[maxn];
int main()
{
    int n,m;
    while(scanf("%d",&n)!=EOF)
    {
        ac.init();
        while(n--)
        {
            scanf("%s",str);
            int len=strlen(str);
            while(str[len-1]=='=') len--;
            for(int i=0;i<len;i++) ss[i]=cal(str[i]);
            int len2 = change(ss,len);
            ac.insert_(aa,len2);//转换之后插入AC自动机
        }
        ac.build();
        scanf("%d",&m);
        while(m--)
        {
            //gets(str);
            scanf("%s",str);
            int len=strlen(str);
            while(str[len-1]=='=') len--;
            for(int i=0;i<len;i++) ss[i]=cal(str[i]);
            int len2 = change(ss,len);
            printf("%d\n",ac.query(aa,len2));
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值