AC自动机板子(hdu2222)&(hdu2896)

参考博客:https://blog.csdn.net/bestsort/article/details/82947639

AC代码:hdu2222

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
const int MOD=100000007;
const int inf=0x3f3f3f3f;
const LL inff=0x3f3f3f3f3f3f3f3f;
const LL N=15;
const LL M=15;
#define MEF(x) memset(x,-1,sizeof(x))
#define ME0(x) memset(x,0,sizeof(x))
#define MEI(x) memset(x,inf,sizeof(x))
struct Auto_c
{
    int tire[500005][26],fail[500005],cnt[500005];
    //tire  字典树
    //fail  失配指针
    //cnt   记录该单词出现次数,记录在单词最后一个字母对应的位置
    int tot;
    void init()
    {
        tot=0;
        ME0(tire);
        ME0(fail);
        ME0(cnt);
    }
    void insertf(char *ss)
    {
        int p=0,len=strlen(ss);
        for(int i=0;i<len;i++)
        {
            if(!tire[p][ss[i]-'a'])
            {
                tire[p][ss[i]-'a']=++tot;
            }
            p=tire[p][ss[i]-'a'];
        }
        cnt[p]++;
    }
    void getfail()
    {
        queue<int> q;
        for(int i=0;i<26;i++)
        {
            if(tire[0][i])
            {
                fail[tire[0][i]]=0;
                q.push(tire[0][i]);
            }
        }
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(int i=0;i<26;i++)
            {
                if(tire[x][i])
                {
                    fail[tire[x][i]]=tire[fail[x]][i];
                    q.push(tire[x][i]);
                }
                else
                {
                    tire[x][i]=tire[fail[x]][i];
                }
            }
        }
    }
    int countf(char *ss)
    {
        int p=0,ans=0;
        int len=strlen(ss);
        for(int i=0;i<len;i++)
        {
            p=tire[p][ss[i]-'a'];
            int temp=p;
            while(temp&&cnt[temp]!=-1)
            {
                ans+=cnt[temp];
                cnt[temp]=-1;//标记  防止重复计算
                temp=fail[temp];
            }
        }
        return ans;
    }
}ac;
char str[55],s[1000005];
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    for(int t1=1;t1<=t;t1++)
    {
        int n;
        cin>>n;
        ac.init();
        for(int i=1;i<=n;i++)
        {
            cin>>str;
            ac.insertf(str);
        }
        ac.getfail();
        cin>>s;
        cout<<ac.countf(s)<<endl;
    }
    return 0;
}

AC代码:hdu2896

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<cmath>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
const int MOD=100000007;
const int inf=0x3f3f3f3f;
const LL inff=0x3f3f3f3f3f3f3f3f;
const LL N=15;
const LL M=15;
#define MEF(x) memset(x,-1,sizeof(x))
#define ME0(x) memset(x,0,sizeof(x))
#define MEI(x) memset(x,inf,sizeof(x))
struct Node
{
    string st;
    int num;
};
int tire[100005][130],fail[100005],cnt[100005],sl[100005];
int tot;
void init()
{
    tot=0;
//        ME0(tire);
//        ME0(fail);
//        ME0(cnt);
//用memset会爆空间
}
void insertf(Node ss)
{
    string s=ss.st;
    int p=0,len=s.size();
    for(int i=0;i<len;i++)
    {
        int x=s[i];
        if(!tire[p][x])
        {
            tire[p][x]=++tot;
        }
        p=tire[p][x];
    }
    cnt[p]=ss.num;
}
void getfail()
{
    queue<int> q;
    for(int i=0;i<=128;i++)
    {
        if(tire[0][i])
        {
            fail[tire[0][i]]=0;
            q.push(tire[0][i]);
        }
    }
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=0;i<=128;i++)
        {
            if(tire[x][i])
            {
                fail[tire[x][i]]=tire[fail[x]][i];
                q.push(tire[x][i]);
            }
            else
            {
                tire[x][i]=tire[fail[x]][i];
            }
        }
    }
}
int countf(Node ss)
{
    int p=0,ans=0;
    int lens=0;
    string sa=ss.st;
    int len=sa.size();
    for(int i=0;i<len;i++)
    {
        int x=sa[i];
        p=tire[p][x];
        int temp=p;
        while(temp)
        {
            ans+=cnt[temp];
            if(cnt[temp]!=0)
            {
                sl[lens++]=cnt[temp];
            }
            temp=fail[temp];
        }
    }
    if(ans!=0)
    {
        sort(sl,sl+lens);
        cout<<"web "<<ss.num<<": ";
        for(int i=0;i<lens;i++)
        {
            if(i==0)
            {
                cout<<sl[i];
            }
            else
            {
                cout<<' '<<sl[i];
            }
        }
        cout<<endl;
    }
    return ans;
}
Node str;
int main()
{
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n;
    init();
    for(int i=1;i<=n;i++)
    {
        cin>>str.st;
        str.num=i;
        insertf(str);
    }
    getfail();
    cin>>m;
    int anss=0;
    for(int i=1;i<=m;i++)
    {
        cin>>str.st;
        str.num=i;
        if(countf(str)!=0)
        {
            anss++;
        }
    }
    cout<<"total: "<<anss<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python AC动机是一个用于字符串匹配的算法,它可以高效地在一段文本中查找多个预定义的模式。它的实现可以使用多种库,其中包括ac动机python和ahocorasick-python。 ac动机python是一个对标准的ac动机算法进行了完善和优化的实现,适用于主流的Python发行版,包括Python2和Python3。它提供了更准确的结果,并且可以通过pip进行安装,具体的安装方法可以参考官方文档或者使用pip install命令进行安装。 ahocorasick-python是另一个实现AC动机的库,它也可以用于Python2和Python3。你可以通过官方网站或者GitHub源码获取更多关于该库的信息和安装指南。 对于AC动机的使用,一个常见的例子是在一段包含m个字符的文章中查找n个单词出现的次数。要了解AC动机,需要有关于模式树(字典树)Trie和KMP模式匹配算法的基础知识。AC动机的算法包括三个步骤:构造一棵Trie树,构造失败指针和模式匹配过程。在构造好AC动机后,可以使用它来快速地在文本中查找预定义的模式,并统计它们的出现次数。&lt;span class=&quot;em&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;em&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;em&quot;&gt;3&lt;/span&gt; #### 引用[.reference_title] - *1* [ahocorasick-python:AC动机python的实现,并进行了优化。 主要修复了 查询不准确的问题](https://download.csdn.net/download/weixin_42122986/18825869)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2&quot;}}] [.reference_item style=&quot;max-width: 50%&quot;] - *2* *3* [Python实现多模匹配&mdash;&mdash;AC动机](https://blog.csdn.net/zichen_ziqi/article/details/104246446)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2&quot;}}] [.reference_item style=&quot;max-width: 50%&quot;] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值