BZOJ[1195][HNOI2006]最短母串 AC自动机+状态压缩

33 篇文章 0 订阅
7 篇文章 0 订阅

传送门ber~

AC自动机多记录一个 state s t a t e 表示这个点代表的状态
把所有串插进AC自动机里,串 i i 的末尾的state就是 2i1 2 i − 1
构建 Fail F a i l 的时候顺便继承所有后缀的 state s t a t e

然后从根按字典序bfs,每走到一个点继承他的 state s t a t e 直到包含所有串就可以啦

贴这么丑的代码真的是对不起大家的眼睛QAQ:

#include<cstring>
#include<ctype.h>
#include<cstdio>
#include<queue>
#define N 2500050
#define mp make_pair
#define pr pair<Node*,pair<int,int> >
using namespace std;
inline int read(){
    int x=0,f=1;char c;
    do c=getchar(),f=c=='-'?-1:f; while(!isdigit(c));
    do x=(x<<3)+(x<<1)+c-'0',c=getchar(); while(isdigit(c));
    return x*f;
}
int n,k,cnt,id,state;
int fa[N],a[N];
char s[55];
bool b[1020][1<<12];
struct Node{
    Node *ch[26],*nex;
    int state,id;///这个点包含串的状态,节点编号
    Node():state(0),nex(NULL),id(++cnt){
        for(int i=0;i<26;i++)
            ch[i]=NULL;
    }
}*root=new Node,*node;
void print(int x){
    if(!x) return;
    print(fa[x]);
    printf("%c",a[x]+'A');
}
struct Aho_Corasick_Automaton{
    inline void Insert(char *s,int id){
        int len=strlen(s+1);
        Node *x=root;
        for(int i=1;i<=len;i++){
            if(!x->ch[s[i]-'A']) x->ch[s[i]-'A']=new Node;
            x=x->ch[s[i]-'A'];
        }
        x->state|=1<<id>>1;
    }
    inline void GetFail(){
        queue<Node*>q;
        for(int i=0;i<26;i++)
            if(!root->ch[i]) root->ch[i]=root;
            else root->ch[i]->nex=root,q.push(root->ch[i]);
        while(!q.empty()){
            Node *x=q.front();q.pop();
            for(int i=0;i<26;i++)
                if(x->ch[i]) x->ch[i]->nex=x->nex->ch[i],q.push(x->ch[i]);
                else x->ch[i]=x->nex->ch[i];
            Node *tmp=x->nex;
            while(tmp!=root && !tmp->state) tmp=tmp->nex;
            x->state|=tmp->state;///继承所有后缀的状态
        }
    }
    inline void solve(){
        queue<pr>q;
        q.push(mp(root,mp(root->state,cnt=0)));
        b[root->id][root->state]=true;
        while(!q.empty()){
            pr x=q.front();q.pop();
            id=x.second.second;
            state=x.second.first;
            node=x.first;
            if(state==(1<<n)-1){///找到满足的,输出
                print(id);
                return;
            }
            for(int i=0;i<26;i++){
                k=state|node->ch[i]->state;///新转移出的状态
                if(b[node->ch[i]->id][k]) continue;///不走重复的
                b[node->ch[i]->id][k]=true;
                fa[++cnt]=id;a[cnt]=i;///记录路径
                q.push(mp(node->ch[i],mp(k,cnt)));
            }
        }
    }
}AC;
int main(){
    n=read();
    for(int i=1;i<=n;i++){
        scanf("%s",s+1);
        AC.Insert(s,i);
    }
    AC.GetFail();///搞Fail指针
    AC.solve();
return 0;
}
BZOJ 2908 题目是一个数据下载任务。这个任务要求下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要求。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值