洛谷P3808 AC自动机复杂版

博客讲述了在解决洛谷P3808题目时遇到的一个复杂版AC自动机问题,作者在处理特定鬼畜数据时遇到了困难,详细描述了调试过程中的困惑和最终解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这题有个数据非常鬼畜,反正我的板子莫名会wa一个地方,改改虽然对了但是还是有点懵逼。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
#include<cstring>
#include<string>
#include<map>
using namespace std;
struct node{
    node*next1[26];
    node*fail;
    int tag,cnt;string strinfo;
    node(){
        memset(next1,0,sizeof(next1));fail=NULL;tag=0;cnt=0;
    }
}*root=new node();
void insert(string str)
{
    node*p=root;
    for(int i=0;i<str.size();i++){
        int x=str[i]-'a';
        if(!p->next1[x]){
            node*t=new node();p->next1[x]=t;
        }
        p=p->next1[x];
    }
    (p->tag)++;p->strinfo=str;
    //if(str=="ab")cout<<1<<endl;
}
void build_fail_pointer(void)
{
    node*p,*temp;
    queue<node*>que;que.push(root);
    while(!que.empty()){
        temp=que.front();que.pop();
        for(int i=0;i<26;i++){
            if(temp->next1[i]){
                if(temp==root)temp->next1[i]->fail=root;
                else{
                    p=temp->fail;
                    while(p){
                        if(p->next1[i]){
                            temp->next1[i]->fail=p->next1[i];break;
                        }
                        p=p->fail;
                    }
                    if(!p)temp->next1[i]->fail=root;
                }
                que.push(temp->next1[i]);
            }
        }
    }
}
void ac_automation(string str)
{
    node*p=root;
    for(int i=0;i<str.size();i++){
        int x=str[i]-'a';
        while(!p->next1[x]&&p!=root)p=p->fail;
        p=p->next1[x];
        if(!p)p=root;
        node*temp=p;
        while(temp!=root){
            if(temp->tag){
                //if(temp->strinfo=="ab")cout<<temp->cnt<<endl;
                //cout<<temp->strinfo<<endl;
                temp->cnt++;
            }
            //else break;这一句一定不能要!!!!!!!!!!!!!!!
            temp=temp->fail;
        }
    }
}
void dl(node*p)//清除所有节点
{
    for(int i=0;i<26;i++){
        if(p->next1[i])dl(p->next1[i]);
    }
    if(p!=root)delete p;
}
int maxn=0;vector<string>ans;
void find(node *p)
{
    for(int i=0;i<26;i++){
        if(p->next1[i]){
            find(p->next1[i]);
        }
    }
    if(p->cnt){
        //if(p->strinfo=="ab")cout<<p->cnt<<endl;
        if(p->cnt>maxn){//如果大于现有答案,清空以前的答案
            maxn=p->cnt;ans.clear();
            for(int i=1;i<=p->tag;i++)
                ans.push_back(p->strinfo);
        }
        else if(p->cnt==maxn)
            for(int i=1;i<=p->tag;i++)
                ans.push_back(p->strinfo);
    }
}
int main()
{
    int n,i,j;string str;
    while(cin>>n&&n){
        map<string,int>m;int shunxu=0;//map保存加入顺序
        maxn=0;ans.clear();dl(root);
        for(i=0;i<26;i++)root->next1[i]=NULL;root->fail=NULL;root->cnt=0;root->tag=0;
        for(i=1;i<=n;i++){
            cin>>str;insert(str);m.insert(pair<string,int>(str,++shunxu));
        }
        build_fail_pointer();
        cin>>str;
        //cout<<str<<endl;
        ac_automation(str);find(root);
        cout<<maxn<<endl;
        map<int,string>m1;
        for(auto i=ans.begin();i!=ans.end();i++){
            string strt=*i;
            auto j=m.find(strt);m1.insert(pair<int,string>(j->second,strt));//按照加入顺序再次丢进一个从小到大的map中,方便按顺序输出
            m.erase(j);
        }
        for(auto i=m1.begin();i!=m1.end();i++)
            cout<<i->second<<endl;
    }

    return 0;
}

鬼畜数据:

10
qabqks
vimbirqy
cflwvxtp
klljfj
ab
nkeiid
fkypjfev
yvgp
evdhs
xaizql
qabqksatffqpjomzstjabfklljfjqevdhsqabqkscflwvxtpeevdhsmzonkeiid

标准输出:

3
ab

如果加上71行那句不能要的就会疯狂wa,一脸懵逼….

### 关于洛谷 P3167 使用 AC 自动机的解题思路 对于洛谷 P3167 的求解,可以采用 AC 自动机来处理多模式字符串匹配问题。此算法不仅能够高效地构建 Trie 树结构用于存储所有模式串,还通过建立失败指针(fail)使得能够在遇到不匹配情况时快速跳转到可能继续匹配的位置。 #### 构建Trie树并插入单词 为了实现这一目标,在初始化阶段需先遍历每一个给定的关键字,并将其逐字符加入至已有的前缀树(Trie Tree)当中形成节连接关系[^1]。当某个新字母未能找到对应的子节时,则创建新的分支;而一旦完成整个关键字路径铺设之后便标记终表示存在以此序列结尾的有效词条。 ```cpp void insert(const string &s){ int p = root; for(auto c : s){ int u = c - 'a'; if(!tr[p][u]) tr[p][u] = ++idx; p = tr[p][u]; } cnt[p]++; } ``` #### 建立fail指针 接下来要做的就是利用队列广度优先搜索(BFS)的方式为每个非根节分配其失配后的转移位置即 fail 指向。具体做法是从根开始一层层向下扩展直到覆盖全部内部顶为止。每当从当前考察对象出发沿某条边e到达另一端v时,若发现 v 已经被访问过则直接令 `fail[v]=get_fail(u)` 后更新其他属性如 end 数组记录经过此处能结束几个词等信息;反之应当把 v 加入等待列表以便后续操作[^2]。 ```cpp queue<int> q; for (int i = 0; i < A; i++) { int t = tr[root][i]; if(t){q.push(t);fail[t]=root;} } while (!q.empty()) { auto t=q.front();q.pop(); for(int i=0;i<A;i++){ int j=tr[t][i]; if(!j){tr[t][i]=tr[fail[t]][i];continue;} q.push(j); fail[j]=tr[fail[t]][i]; } } ``` #### 查询过程中的应用 最后进入查询环节,此时输入待检验文本流text并通过循环读取其中各个组成成分尝试在先前搭建好的框架内定位是否存在相吻合的部分。每成功推进一位都意味着找到了至少一个公共开头片段,与此同时借助之前设定好的辅助机制(比如end[])统计出实际命中次数作为输出结果的一部分呈现出来[^3]。 ```cpp int query(string str) { int res=0,p=root; for(char ch:str){ int d=ch-'a'; p=tr[p][d]; int temp=p; while(temp!=root){ res+=cnt[temp]; cnt[temp]=0;//防止重复计算 temp=fail[temp]; } } return res; } ``` 上述代码展示了如何运用AC自动机解决洛谷P3167的问题核心部分———即构造Trie图、设置失效函数以及执行查找任务的具体流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值