ZOJ 3430 Detect the Virus (AC自动机)

题目链接:Detect the Virus



题意:n个模式串,一个文本串,问文本串包含几个模式串。

解析:解码 + AC自动机。

       解码过程:先将字符串转换成ASCII 然后根据相应的ASCII 转换成二进制,每一个是6位,不够加0,然后取8位为一个字符,求得的字符串为要的字符串。

PS:注意sigma_size = 256


AC代码:

#include <bits/stdc++.h>
using namespace std;

struct Trie{
    int next[520*64][256], fail[520*64], end[520*64];
    int root, L;

    int newnode(){
        for(int i = 0; i < 256; i++) next[L][i] = -1;
        end[L++] = -1;
        return L-1;
    }

    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(next[now][ buf[i] ] == -1) next[now][ buf[i] ] = newnode();
            now = next[now][ buf[i] ];
        }
        end[now] = id;
    }

    void build(){
        queue<int> Q;
        fail[root] = root;
        for(int i = 0; i < 256; i++){
            if(next[root][i] == -1) next[root][i] = root;
            else{
                fail[ next[root][i] ] = root;
                Q.push(next[root][i]);
            }
        }
        while(!Q.empty()){
            int now = Q.front();
            Q.pop();
            for(int i = 0; i < 256; i++){
                if(next[now][i] == -1) next[now][i] = next[ fail[now] ][i];
                else{
                    fail[ next[now][i] ] = next[ fail[now] ][i];
                    Q.push(next[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 = next[now][ buf[i] ];
            int temp = now;
            while(temp != root){
                if(end[temp] != -1) used[ end[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(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif //sxk

    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));
        }
        puts("");
    }
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是ZOJ1626的C++ AC代码,使用了旋转卡壳算法: ```c++ #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #define MAXN 100010 #define eps 1e-8 #define INF 1e20 using namespace std; struct point { double x,y; friend point operator -(point a,point b) { point res; res.x=a.x-b.x; res.y=a.y-b.y; return res; } friend bool operator <(point a,point b) { if(fabs(a.x-b.x)<eps) return a.y<b.y; return a.x<b.x; } friend double operator *(point a,point b) { return a.x*b.y-a.y*b.x; } friend double dis(point a,point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } }a[MAXN],b[MAXN],st[MAXN]; int n; double ans=INF; int cmp(point a,point b) { double tmp=(a-b)*(a[1]-b); if(fabs(tmp)<eps) return dis(a,a[1])-dis(b,a[1])<0; return tmp>0; } int main() { while(~scanf("%d",&n) && n) { for(int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y); sort(a+1,a+n+1); int tot=0; for(int i=1;i<=n;i++) { while(tot>=2 && (st[tot]-st[tot-1])*(a[i]-st[tot])<0) tot--; st[++tot]=a[i]; } int k=tot; for(int i=n-1;i>=1;i--) { while(tot>k && (st[tot]-st[tot-1])*(a[i]-st[tot])<0) tot--; st[++tot]=a[i]; } tot--; for(int i=1;i<=tot;i++) b[i]=st[i]; int tmp=1; for(int i=2;i<=tot;i++) if(b[i].y<b[tmp].y) tmp=i; swap(b[1],b[tmp]); sort(b+2,b+tot+1,cmp); st[1]=b[1]; st[2]=b[2]; k=2; for(int i=3;i<=tot;i++) { while(k>1 && (st[k]-st[k-1])*(b[i]-st[k])<=0) k--; st[++k]=b[i]; } double ans=0; if(k==2) ans=dis(st[1],st[2]); else { st[k+1]=st[1]; for(int i=1;i<=k;i++) for(int j=1;j<=k;j++) ans=max(ans,dis(st[i],st[j])); } printf("%.2lf\n",ans/2); } return 0; } ``` 其中,结构体 `point` 表示二维平面上的一个点,包含了点的坐标和一些基本操作。函数 `cmp` 是旋转卡壳算法中的比较函数,按照点到起点的极角从小到大排序。在主函数中,先使用 Graham 扫描法求出点集的凸包,然后按照旋转卡壳的步骤,求出凸包上的最远点对距离作为最小直径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值