zoj 3430 Detect the Virus 【AC自动机】【坑死。。。】

Detect the Virus

Time Limit: 2 Seconds       Memory Limit: 65536 KB

One day, Nobita found that his computer is extremely slow. After several hours' work, he finally found that it was a virus that made his poor computer slow and the virus was activated by a misoperation of opening an attachment of an email.

Nobita did use an outstanding anti-virus software, however, for some strange reason, this software did not check email attachments. Now Nobita decide to detect viruses in emails by himself.

To detect an virus, a virus sample (several binary bytes) is needed. If these binary bytes can be found in the email attachment (binary data), then the attachment contains the virus.

Note that attachments (binary data) in emails are usually encoded in base64. To encode a binary stream in base64, first write the binary stream into bits. Then take 6 bits from the stream in turn, encode these 6 bits into a base64 character according the following table:

That is, translate every 3 bytes into 4 base64 characters. If the original binary stream contains 3k + 1 bytes, where k is an integer, fill last bits using zero when encoding and append '==' as padding. If the original binary stream contains 3k + 2 bytes, fill last bits using zero when encoding and append '=' as padding. No padding is needed when the original binary stream contains 3k bytes.

Value012345678910111213141516171819202122232425262728293031
EncodingABCDEFGHIJKLMNOPQRSTUVWXYZabcdef
Value3233343536373839404142434445464748495051525354555657585960616263
Encodingghijklmnopqrstuvwxyz0123456789+/

For example, to encode 'hello' into base64, first write 'hello' as binary bits, that is: 01101000 01100101 01101100 01101100 01101111
Then, take 6 bits in turn and fill last bits as zero as padding (zero padding bits are marked in bold): 011010 000110 010101 101100 011011 000110 111100
They are 26 6 21 44 27 6 60 in decimal. Look up the table above and use corresponding characters: aGVsbG8
Since original binary data contains 1 * 3 + 2 bytes, padding is needed, append '=' and 'hello' is finally encoded in base64: aGVsbG8=

Section 5.2 of RFC 1521 describes how to encode a binary stream in base64 much more detailedly:

Click here to see Section 5.2 of RFC 1521 if you have interest

Here is a piece of ANSI C code that can encode binary data in base64. It contains a function, encode (infile, outfile), to encode binary file infile in base64 and output result to outfile.

Click here to see the reference C code if you have interest
Input

Input contains multiple cases (about 15, of which most are small ones). The first line of each case contains an integer N (0 <= N <= 512). In the next N distinct lines, each line contains a sample of a kind of virus, which is not empty, has not more than 64 bytes in binary and is encoded in base64. Then, the next line contains an integer M (1 <= M <= 128). In the following M lines, each line contains the content of a file to be detected, which is not empty, has no more than 2048 bytes in binary and is encoded in base64.

There is a blank line after each case.

Output

For each case, output M lines. The ith line contains the number of kinds of virus detected in the ith file.

Output a blank line after each case.

Sample Input
3
YmFzZTY0
dmlydXM=
dDog
1
dGVzdDogdmlydXMu

1
QA==
2
QA==
ICAgICAgICA=
Sample Output
2

1
0

Hint

In the first sample case, there are three virus samples: base64virus and t: , the data to be checked is test: virus., which contains the second and the third, two virus samples.


原串s1——根据每个字符的ascll码写出所有8位二进制,然后每6位分割一次,根据分割出的二进制化成十进制,在表中找到对应的字符,得到base64串s2。若原串s1占字节为3*k+1,在s2后面添上两个=;若原串s1占字节为3*k+2,在s2后面添上一个=;若原串s1占字节为3*k,不进行任何添加操作。


题意:给你n个base64目标串和m个文本串,问你文本串中出现多少种目标串?


用字符串WA到死,ORZ下bin神,o(╯□╰)o太渣了。


思路:将base64字符串解码成原串,解码坑死我了。然后跑AC自动机,把存在串的编号标记上,最后统计出所有标记的串数。 


AC代码:


#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#define MAXN 60000+10
using namespace std;
struct Trie
{
    int next[MAXN][256], fail[MAXN], End[MAXN];
    int L, root;
    int newnode()
    {
        for(int i = 0; i < 256; i++)//256个分支。。。
            next[L][i] = -1;
        End[L++] = -1;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void Insert(unsigned char *s, int len, int id)
    {
        int now = root;
        for(int i = 0; i < len; i++)
        {
            if(next[now][s[i]] == -1)
                next[now][s[i]] = newnode();
            now = next[now][s[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 flag[600];
    int Query(unsigned char *s, int len, int n)
    {
        int now = root;
        int ans = 0;
        memset(flag, false, sizeof(flag));
        for(int i = 0; i < len; i++)
        {
            now = next[now][s[i]];
            int temp = now;
            while(temp != root)
            {
                if(End[temp] != -1)
                    flag[End[temp]] = true;
                temp = fail[temp];
            }
        }
        for(int i = 1; i <= n; i++)
            ans += flag[i] ? 1 : 0;
        return ans;
    }
};
Trie ac;
unsigned char buf[2050];
int top;
char str[4000];
unsigned char s[4000];
unsigned char Get(char ch)
{
    if(ch >= 'A' && ch <= 'Z')
        return ch - 'A';
    else if(ch >= 'a' && ch <= 'z')
        return ch - 'a' + 26;
    else if(ch >= '0' && ch <= '9')
        return ch - '0' + 52;
    else 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];
    }
    top = t;
}
int main()
{
    int n, m;
    while(scanf("%d",&n) == 1)
    {
        ac.init();
        for(int i = 1; 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, top, 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, top, n));
        }
        printf("\n");
    }
    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、付费专栏及课程。

余额充值