AC自动机训练(16.04.01)

复习了AC自动机,记录几题,虽然有些之前做过,但是这次又写了一遍后感觉有了新的认识(以前写的不好的直接删除了 ^_^)。外加一道和AC自动机没有半毛钱关系的模拟题。
hdu 2222 Keywords Search
hdu 2896 病毒侵袭
zoj 3228 Searching the String
hdu 2778 LCR (模拟题)

http://acm.hdu.edu.cn/showproblem.php?pid=2222
大意:找出字符串中包含几个关键字
分析:简单AC自动机,再写一次此题是因为这次注释写的比以往详细^_^。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=1e6+10;
struct node{
    int sum;
    node *fail,*next[26];
    node(){
        sum=0;
        fail=NULL;
        for(int i=0;i<26;i++) next[i]=NULL;
    }
};
node *root;
void w_insert(char str[]){
    node *p=root;
    for(int i=0;str[i];i++){
        int dex=str[i]-'a';
        if(p->next[dex]==NULL){
            p->next[dex]=new node();
        }
        p=p->next[dex];
    }
    p->sum++;  //记录字符串的个数
}
node *que[N];  //指针数组
void fail_point(){
    int head=0,tail=0;
    que[tail++]=root;
    while(head<tail){
        node *p=que[head++], *temp=NULL;
        for(int i=0;i<26;i++){
            if(p->next[i]){
                if(p==root) p->next[i]->fail=root;  // 第一个字符就不匹配
                else {
                    temp=p->fail;  // 上一个结点
                    while(temp){
                        if(temp->next[i]){
                           p->next[i]->fail=temp->next[i]; //失败指针指向相同字符
                           break;
                        }
                        temp=temp->fail;  //不断找到和上个字符一样的结点
                    }
                    if(temp==NULL) p->next[i]->fail=root;  //找不到用结点
                }
                que[tail++]=p->next[i];  //入队
            }
        }
    }
}
int query(char *s){  //查询模式串(短串)的个数
    int ans=0;
    node *p=root;
    for(int i=0;s[i];i++){
        int dex=s[i]-'a';
        while(p!=root && p->next[dex]==NULL) p=p->fail;  // 找到适合点,防止非法内存的访问
        p=p->next[dex];
        if(p==NULL) p=root;  // 经过所有的相同前缀,始终找不到
        node *temp=p;  // 防止乱了
        while(temp!=root && temp->sum!=-1){ // fail的极限是root
            ans+=temp->sum;   // 设-1是判断条件, 0依然可以是可能串的前缀
            temp->sum=-1;
            temp=temp->fail;
        }
    }
    return ans;
}
char s[55],str[N];
int main()
{
    //freopen("cin.txt","r",stdin);
    int t,n;
    cin>>t;
    while(t--){
        root=new node();  //开辟新地址
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s",s);
            w_insert(s);
        }
        fail_point();
        scanf("%s",str);
        printf("%d\n",query(str));
    }
    return 0;
}

hdu 2896 病毒侵袭

http://acm.hdu.edu.cn/showproblem.php?pid=2896
大意:
……
第一行,一个整数N(1<=N<=500),表示病毒特征码的个数。
接下来N行,每行表示一个病毒特征码,特征码字符串长度在20―200之间。
每个病毒都有一个编号,依此为1―N。
不同编号的病毒特征码不会相同。
在这之后一行,有一个整数M(1<=M<=1000),表示网站数。
接下来M行,每行表示一个网站源码,源码字符串长度在7000―10000之间。
每个网站都有一个编号,依此为1―M。
以上字符串中字符都是ASCII码可见字符(不包括回车)。
分析:
ASCII可见字符:32-126(95个,其中32是空格)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=100, M=1e5+10; 
struct node{
    int id;
    node *next[N],*fail;
    node(){
        id=0;
        fail=NULL;
        memset(next,0,sizeof(next));
    }
};
node *root;
void w_insert(char *s,int dex){
    node *p=root;
    for(int i=0;s[i];i++){
        int d=s[i]-32;
        if(p->next[d]==NULL) p->next[d]=new node();
        p=p->next[d];
    }
    p->id=dex; 
}
node *que[M];
void fail_point(){
    int head=0,tail=0;
    que[tail++]=root;
    while(head<tail){
        node *p=que[head++], *temp=NULL;
        for(int i=0;i<N;i++){
            if(p->next[i]){
                if(p==root) p->next[i]->fail=root;
                else {
                    temp=p->fail;
                    while(temp){
                        if(temp->next[i]){
                            p->next[i]->fail=temp->next[i];
                            break;
                        }
                        temp=temp->fail;
                    }
                    if(temp==NULL) p->next[i]->fail=root; 
                }
                que[tail++]=p->next[i];
            }
        }
    }
}
int web[505],top;
bool tag[505];
void query(char *s){
    top=0;
    memset(tag,0,sizeof(tag));
    node *p=root;
    for(int i=0;s[i];i++){
        int d=s[i]-32;
        while(p!=root && p->next[d]==NULL) p=p->fail;
        p=p->next[d];
        if(p==NULL) p=root;
        node *temp=p;
        while(temp!=root){
           if(tag[temp->id]==0 && temp->id){
               web[top++]=temp->id;
               tag[temp->id]=1; 
            }
            temp=temp->fail;
        }
    }
}
char s[205],str[10010];
int main()
{
    //freopen("cin.txt","r",stdin);
    int n,m;
    while(cin>>n){
        root=new node();
        for(int i=1;i<=n;i++) {
            scanf("%s",s);
            w_insert(s,i);
        }
        fail_point();
        scanf("%d",&m);
        int total=0;
        for(int i=1;i<=m;i++){
            scanf("%s",str);
            query(str);
            sort(web,web+top);  // virus order arise
            if(top){
                total++;
                printf("web %d: ",i);
                for(int j=0;j<top-1;j++) printf("%d ",web[j]);
                printf("%d\n",web[top-1]);
            }
        }
        printf("total: %d\n",total);
        delete root;
    }
    return 0;
}

zoj 3228 Searching the String

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3441
大意:给出一些短串,求得在长串中有多少的特征短串。如果标志值tag是0,则允许在长串中相同短串重叠统计,如果tag是1则不允许。
分析:和一般的多串匹配相比这题的难点在于有时可以重叠统计,有时又不允许重叠统计。题目最开始给出长串,可能一开始向这样的方向思考:把所有的特征短串写入trie树,然后AC自动机模式匹配。恩,成功的走向了死胡同。因为用长串来查找统计短串的个数很有可能一开始就得到0。 正确的思路是把长串上所有的子串都放进trie树,然后拿每一个短串来匹配统计自身在长串中出现的次数。我们在插入特征短串的时候就记录所有字符的overlap和not overlap的结果。最后根据tag直接找到短串输出即可。
hit: 使用STL queue可以防止MLE。
<此题之前也写过:http://blog.csdn.net/thearcticocean/article/details/47165197>

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N=26,M=6e5+10;
char s[10],str[100010];
struct node{
    int id,tag[2];
    node *next[N],*fail;
    node(){
        id=-1;
        tag[0]=tag[1]=0;
        fail=NULL;
        memset(next,0,sizeof(next));
    }
};
node *root;
void w_insert(int dex){
    node *p=root;
    for(int i=dex;i<dex+6 && str[i];i++){
        int d=str[i]-'a';
        if(p->next[d]==NULL) p->next[d]=new node();
        p=p->next[d];
        p->tag[0]++; // allow overlap 
        if(p->id<dex) { // not alow overlap, from front to back 
            p->tag[1]++;
            p->id=i;
        }
    }
}
//node *que[M];
queue<node *> que;
void fail_build(){
    while(!que.empty()) que.pop();
    //int head=0,tail=0;
    //que[tail++]=root;
    que.push(root);
    while(!que.empty()){
        node *p=que.front(), *temp=NULL;
        que.pop();
        for(int i=0;i<N;i++){
            if(p->next[i]){
                 if(p==root) p->next[i]->fail=root;
                 else {
                      temp=p->fail;
                      while(temp){
                         if(temp->next[i]){
                             p->next[i]->fail=temp->next[i]; 
                             break; 
                          }
                          temp=temp->fail;
                      }
                      if(temp==NULL) p->next[i]->fail=root;
                  }
                  que.push(p->next[i]);
             }
        }
    }
}
int query(int t){
    node *p=root;
    for(int i=0;s[i];i++){
        int d=s[i]-'a';
        while(p->next[d]==NULL) return 0;
        p=p->next[d];
    }
    return p->tag[t];
}
void del(node *p){  
     if(p==NULL)return ;  
     for(int i=0;i<N;i++)del(p->next[i]);  
     delete p;  
}  
int main(){
    //freopen("cin","r",stdin);
    int n,t;
    int ca=1;
    while(~scanf("%s",str)){
        root=new node();
        for(int i=0;str[i];i++) w_insert(i); // enter every one word 
        fail_build();
        scanf("%d",&n);
        printf("Case %d\n",ca++);
        for(int i=0;i<n;i++){
            scanf("%d%s",&t,s);
            printf("%d\n",query(t));
        }
        puts("");
        del(root);
    }
    return 0;
}

hdu 2778 LCR (模拟题)

http://acm.hdu.edu.cn/showproblem.php?pid=2778
大意:多个人玩一个游戏,一开始所有的人都有3个chips,从第一个人开始执筛子,最多执3次,如果chips少于3那就执chips次。面是L,将自己的一个chip交给左边的人,i+1那个人;面是R,将自己的一个chips交给右边的人,i-1那个人;面是C,将自己的一个chip放到center中。如果一个人拥有了除center里的所有chips,那么他就赢了,游戏结束,即使字符串没有读完也结束。如果字符串剩下的字符不足以进行投筛子那么也结束
分析:模拟求之

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=1e6+10;
char s[N];
int f[13],center;
int check(int n){
    int over=0;
    for(int i=0;i<n;i++) if(f[i]) over++;
    return over;
}
int main(){
    //freopen("cin.txt","r",stdin);
    int n,ca=0;
    while(cin>>n&&n){
        scanf("%s",s);
        for(int i=0;i<n;i++) f[i]=3;
            center=0;
            int p=0,temp=0,L=strlen(s);
            int over,i;
            for(i=0;s[i];){
            over=check(n);
            if(over==1) break; 
            temp=f[p];
            if(temp>3) temp=3;  // 最多执三次
            if(temp>L-i) break;
            while(temp && s[i]){
                if(s[i]=='L'){ f[(p+1)%n]++; f[p]--; }
                else if(s[i]=='R'){  f[(p-1+n)%n]++; f[p]--; }
                else if(s[i]=='C'){  center++; f[p]--; }
                temp--;  i++;
                over=check(n);
                if(over==1) break; 
            }
            if(over==1) break;
            p=(p+1)%n;
        }
        while(over>1 && f[p]==0) p=(p+1)%n;  // 找到下一个人
        if(ca) puts(""); 
        printf("Game %d:\n",++ca);
        for(int i=0;i<n;i++){
            if(over==1) {
                if(f[i]) printf("Player %d:%d(W)\n",i+1,f[i]);
                else printf("Player %d:0\n",i+1);
            }
            else {
                if(i==p) printf("Player %d:%d(*)\n",i+1,f[i]);
                else printf("Player %d:%d\n",i+1,f[i]);
            }
        }
        printf("Center:%d\n",center); 
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值