ZOJ 3430 Detect the Virus(AC自动机+解码)

超级传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430


本题思路清晰,不过解码确实繁琐,看来自己代码能力还有待加强。

需要注意几个点:解码后的ASCII字符的范围是0~255,而且可能包含'\0',不能用char来存,最好用int。


代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>

#define STATIC_AC_MOTON

using namespace std;

typedef int Type;

const int char_count = 256;
const char base_char = 0;

typedef struct __node
{
    Type val;
    int node_data;
    int vis;
    struct __node* next[char_count];
    struct __node* parent;
    struct __node* prefix;
}
Node;

#ifdef DYNAMIC_AC_MOTON
Node* rt;
#endif

#ifdef STATIC_AC_MOTON
const int max_node_count = 512 * 64 + 10;
Node node[max_node_count];
Node* rt = &node[0];
int node_count = 1;
#endif

int text[8 * 2048 + 10];
char _str[2048 + 10];

void createTree(Node** root)
{
#ifdef DYNAMIC_AC_MOTON
    *root = (Node*)malloc(sizeof(Node));
#endif
    (*root)->val = 0;
    (*root)->parent = NULL;
    (*root)->prefix = NULL;
    (*root)->vis = 0;
    (*root)->node_data = base_char;
    for (int i = 0; i < char_count; i++)
        (*root)->next[i] = NULL;
}

void deleteTree(Node* root)
{
#ifdef DYNAMIC_AC_MOTON
    if (root == NULL)
        return;
    for (int i = 0; i < char_count; i++)
    {
        if (root->next[i] != NULL)
            deleteTree(root->next[i]);
    }
    free(root);
#endif
#ifdef STATIC_AC_MOTON
    node_count = 1;
#endif
}

void insert(Node** root, int* str, int n)
{
    for (int i = 0; i < n; i++)
    {
        if ((*root)->next[str[i] - base_char] == NULL)
        {
#ifdef DYNAMIC_AC_MOTON
            (*root)->next[str[i] - base_char] = (Node*)malloc(sizeof(Node));
#endif
#ifdef STATIC_AC_MOTON
            (*root)->next[str[i] - base_char] = &node[node_count++];
#endif

            (*root)->next[str[i] - base_char]->parent = *root;
            root = &((*root)->next[str[i] - base_char]);
            (*root)->val = 0;
            (*root)->vis = 0;

            for (int j = 0; j < char_count; j++)
                (*root)->next[j] = NULL;
        }
        else
            root = &((*root)->next[str[i] - base_char]);

        (*root)->node_data = str[i];

        if (i == n - 1)
            (*root)->val++;
    }
}

int query(Node* root, int* text, int n)
{
    int count = 0;

    Node* p = root;
    int pos = 0;

    do
    {
        while (p != NULL && pos < n)
        {
            Node* cnt = p;
            while (cnt != root && !cnt->vis)
            {
                cnt->vis = 1;
                count += cnt->val;
                cnt = cnt->prefix;
            }

            if (p->next[text[pos] - base_char] != NULL)
            {
                p = p->next[text[pos] - base_char];
                pos++;
            }
            else
                p = p->prefix;
        }

        if (p == NULL)
        {
            p = root;
            pos++;
        }
        if (pos == n)
        {
            Node* cnt = p;
            while (cnt != root && !cnt->vis)
            {
                cnt->vis = 1;
                count += cnt->val;
                cnt = cnt->prefix;
            }
        }
    }
    while (pos < n);

    return count;
}

void make_prefix_pointer(Node* root)
{
    queue<Node*> q;
    q.push(root);

    while (!q.empty())
    {
        Node* cnt = q.front();
        q.pop();

        for (int i = 0; i < char_count; i++)
        {
            if (cnt->next[i] != NULL)
                q.push(cnt->next[i]);
        }

        if (cnt == root)
        {
            cnt->prefix = NULL;
            continue;
        }

        if (cnt->parent == root)
        {
            cnt->prefix = root;
            continue;
        }

        Node* pre = cnt->parent->prefix;

        while (pre != NULL)
        {
            if (pre->next[cnt->node_data - base_char] != NULL)
            {
                cnt->prefix = pre->next[cnt->node_data - base_char];
                break;
            }
            else
                pre = pre->prefix;
        }

        if (pre == NULL)
            cnt->prefix = root;
    }
}

void clearVisFlag(Node* root)
{
#ifdef DYNAMIC_AC_MOTON
    if (root == NULL)
        return;
    for (int i = 0; i < char_count; i++)
        clearVisFlag(root->next[i]);
    root->vis = 0;
#endif
#ifdef STATIC_AC_MOTON
    for (int i = 0; i < node_count; i++)
        node[i].vis = 0;
#endif
}

int decodeBase64ToASCII(int* des, char* src)
{
    int len = strlen(src);
    int desIndex = 0;
    int incNum = 0;
    int zeroBitsCount = 0;
    int bits[6];

    for (int i = 0; i < len; i++)
    {
        if (src[i] >= 'A' && src[i] <= 'Z')
            incNum = src[i] - 'A';
        else if (src[i] >= 'a' && src[i] <= 'z')
            incNum = src[i] - 'a' + 26;
        else if (src[i] >= '0' && src[i] <= '9')
            incNum = src[i] - '0' + 52;
        else if (src[i] == '+')
            incNum = 62;
        else if (src[i] == '/')
            incNum = 63;
        else if (src[i] == '=')
        {
            zeroBitsCount = len - i;
            break;
        }

        for (int j = 0; j < 6; j++)
            bits[j] = 0;
        int cntBitIndex = 5;
        while (incNum)
        {
            bits[cntBitIndex--] = incNum % 2;
            incNum >>= 1;
        }

        for (int j = 0; j < 6; j++)
            des[desIndex + j] = bits[j] + '0';
        desIndex += 6;
    }
    desIndex -= zeroBitsCount << 1;

    for (int i = 0; i < desIndex; i += 8)
    {
        int val = 0;
        for (int j = 7; j >= 0; j--)
            val += (des[i + j] - '0') * (1 << (7 - j));
        des[i / 8] = val;
    }
    return desIndex / 8;
}

int main()
{
    int n, m;

    while (scanf("%d", &n) > 0)
    {
        createTree(&rt);
        for (int i = 0; i < n; i++)
        {
            scanf("%s", _str);
            int len = decodeBase64ToASCII(text, _str);
            insert(&rt, text, len);
        }
        make_prefix_pointer(rt);

        scanf("%d", &m);
        for (int i = 0; i < m; i++)
        {
            scanf("%s", _str);
            int len = decodeBase64ToASCII(text, _str);
            printf("%d\n", n ? query(rt, text, len) : 0);
            clearVisFlag(rt);
        }
        printf("\n");
        deleteTree(rt);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值