南阳oj 1085 AC自动机 一般(有点坑)

点击打开题目

题目意思很明确,AC自动机没什么好说的,不过这个题有个地方有点坑,就是有些同样的单词会多次输入,而且输出的时候按照顺序来,只要满足频率最高,就算同样的单词已经输出了还要再输出

AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
using namespace std;
struct Trie
{
	int count;
	int num[20];//记录单词的序号 
	int tot;//可能会有重复的单词 
	struct Trie *next[26];
	struct Trie *fail;
};
struct node
{
	int count;
}a[200];//记录每个序号的单词出现的频率
int Max;
char str[1000001];
Trie *newset()
{
	Trie *p=(Trie*)malloc(sizeof(Trie));
	p->count=p->tot=0;
	p->fail=NULL;
	for(int i=0;i<26;i++)
	p->next[i]=NULL;
	return p;
}
void insert(char s[200],Trie *root,int num)
{
	int i,t,len=strlen(s);
	Trie *p=root;
	for(i=0;i<len;i++)
	{
		t=s[i]-'a';
		if(p->next[t]==NULL)
		p->next[t]=newset();
		p=p->next[t];
	}
	p->count++;
	p->tot++;
	p->num[p->tot]=num;
}
void build_AC(Trie *root)
{
	int i,t;
	Trie *p,*q;
	queue<Trie*>Q;
	Q.push(root);
	while(!Q.empty())
	{
		p=Q.front();
		Q.pop();
		for(i=0;i<26;i++)
		if(p->next[i])
		{
			if(p==root)
			p->next[i]->fail=root;
			else
			{
				q=p->fail;
				while(q)
				{
					if(q->next[i])
					{
						p->next[i]->fail=q->next[i];
						break;
					}
					q=q->fail; 
				}
				if(!q)
				p->next[i]->fail=root;
			}
			Q.push(p->next[i]);
		}
	}
}
void query_AC(Trie *root)
{
	int i,j,t,len=strlen(str);
	Trie *p,*q;
	p=root;
	for(i=0;i<len;i++)
	{
		t=str[i]-'a';
		while(!p->next[t]&&p!=root)
		p=p->fail;
		if(p->next[t])
		{
			p=p->next[t];
			if(p->count)
			{
				for(int k=1;k<=p->tot;k++)//可能会有重复的单词,每个序号都要统计 
				{
					j=p->num[k];
					a[j].count++;
					if(Max<a[j].count)
					Max=a[j].count;	
				}
			}
			q=p;
			while(q)
			{
				q=q->fail;
				if(q&&q!=root&&q->count!=0)
				{
					for(int k=1;k<=q->tot;k++)
					{
						j=q->num[k];
						a[j].count++;
						if(Max<a[j].count)
						Max=a[j].count;				
					}
	
				}
			}
			
		}
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,i;
		Trie *root=newset();
		char s[200][200];
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%s",s[i]);
			insert(s[i],root,i);
			a[i].count=0;
		}
		scanf("%s",str);
		build_AC(root);
		Max=0;
		query_AC(root);
		printf("%d\n",Max);
		for(i=0;i<n;i++)
		if(a[i].count==Max)
		printf("%s\n",s[i]);
	}
}

 

续更,以前的代码竟然又a不掉这个题了,然后再看刘汝佳的大白书重新认识了字典树和ac自动机,借助大佬的思想再ac,不过有个坑点,题目说n最大是150,结果我开155的数组都wa,开200才ac

 

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
#define maxnode 20000
#define p_num 200
int ans;
map<string,int>ms;
struct AC_auto
{
	int cnt[p_num];
	int sz;
	int ch[maxnode][26];
	int f[maxnode];
	int val[maxnode];
	int last[maxnode];
	
	void init()
	{
		sz=1;
		memset(ch[0],0,sizeof(ch[0]));
		memset(cnt,0,sizeof(cnt));
		ms.clear();
	}
	
	void insert(char *s,int v)
	{
		int u=0,n=strlen(s);
		for(int i=0;i<n;i++)
		{
			int c=s[i]-'a';
			if(!ch[u][c])
			{
				memset(ch[sz],0,sizeof(ch[sz]));
				val[sz]=0;
				ch[u][c]=sz++;
			}
			u=ch[u][c];
		}
		val[u]=v;
		ms[string(s)] = v;
	}
	
	void print(int j)
	{
		if(j)
		{
			cnt[val[j]]++;
			ans=max(ans,cnt[val[j]]);
			print(last[j]);
		}
	}
	
	void find(char *T)
	{
		int n=strlen(T);
		int j=0;
		for(int i=0;i<n;i++)
		{
			int c=T[i]-'a';
			while(j&&!ch[j][c]) j=f[j];
			j=ch[j][c];
			if(val[j]) print(j);
			else if(last[j]) print(last[j]);
		}
	}
	
	void getFail()
	{
		queue<int>q;
		f[0]=0;
		for(int c=0;c<26;c++)
		{
			int u=ch[0][c];
			if(u)
			{
				f[u]=0;
				q.push(u);
				last[u]=0;
			}
		}
		while(!q.empty())
		{
			int r=q.front();q.pop();
			for(int c=0;c<26;c++)
			{
				int u=ch[r][c];
				if(!u)
				continue;
				q.push(u);
				int v=f[r];
				while(v&&!ch[v][c])
				v=f[v];
				f[u]=ch[v][c];
				last[u]=val[f[u]]?f[u]:last[f[u]];
			}
		}
	}
};
char str[200][105],s1[1000005];
AC_auto ac;
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,i,j;
		scanf("%d",&n);
		ac.init();
		for(i=1;i<=n;i++)
		{
			scanf("%s",str[i]);
			ac.insert(str[i],i);
		}
		scanf("%s",s1);
		ans=0;
		ac.getFail();
		ac.find(s1);
		printf("%d\n",ans);
		for(i=1;i<=n;i++)
		if(ac.cnt[ms[string(str[i])]]==ans)
		printf("%s\n",str[i]);
	}
}

 

//更新模板

易理解版本:
// http://acm.hdu.edu.cn/showproblem.php?pid=2222
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
struct Tri {
    int ch[maxn][26], val[maxn], fail[maxn], sz;
    void init() {
        memset(ch[0], 0, sizeof(ch[0]));
        sz = 0;
    }
    void insert(char *s) {
        int o = 0;
        for (int i = 0; s[i]; i++) {
            int c = s[i] - 'a';
            if (!ch[o][c]) { //建立新节点 
                ch[o][c] = ++sz;
                val[sz] = 0;
                memset(ch[sz], 0, sizeof(ch[sz]));
            }
            o = ch[o][c];
        }
        val[o]++;
    }
    void build() {
        queue<int> q;
        for (int i = 0; i < 26; i++)
        if (ch[0][i]) {
            q.push(ch[0][i]);
            fail[ch[0][i]] = 0;//第一层节点fail都指向根 
        }
        while (!q.empty()) {
            int o = q.front();
            q.pop();
            for (int i = 0; i < 26; i++)
            if (ch[o][i]) {
                int v = ch[o][i];
                int fa = fail[o];
                while (fa && !ch[fa][i])//如果当前节点没有 i 号儿子,继续跳fail指针,直到根或者有 i 号节点 
                    fa = fail[fa];
                fail[v] = ch[fa][i];
                q.push(v);
            }
        }
    }
    int query(char *s) {
        int o = 0, ans = 0;
        for (int i = 0; s[i]; i++) {
            int c = s[i] - 'a';
            while (!ch[o][c] && o) 
                o = fail[o];
            o = ch[o][c];
            int tmp = o;
            while (tmp) { //每次不仅要记录当前节点的答案,也要记录失败指针指向的节点的答案 
                ans += val[tmp];
                val[tmp] = 0;
                tmp = fail[tmp];
            }
        }
        return ans;
    }
} ac;
char s[maxn];
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        int n;
        ac.init();
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%s", s);
            ac.insert(s);
        }
        ac.build();
        scanf("%s", s);
        printf("%d\n", ac.query(s));
    }
}
//精简版本
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
struct Tri {
    int ch[maxn][26], val[maxn], fail[maxn], sz;
    void init() {
        memset(ch[0], 0, sizeof(ch[0]));
        sz = 0;
    }
    void insert(char *s) {
        int o = 0;
        for (int i = 0; s[i]; i++) {
            int c = s[i] - 'a';
            if (!ch[o][c]) {
                ch[o][c] = ++sz;
                val[sz] = 0;
                memset(ch[sz], 0, sizeof(ch[sz]));
            }
            o = ch[o][c];
        }
        val[o]++;
    }
    void build() {
        queue<int> q;
        for (int i = 0; i < 26; i++)
        if (ch[0][i]) {
            q.push(ch[0][i]);
            fail[ch[0][i]] = 0;
        }
        while (!q.empty()) {
            int o = q.front();
            q.pop();
            for (int i = 0; i < 26; i++)
            if (ch[o][i]) {
                int v = ch[o][i];
                fail[v] = ch[fail[o]][i];
                q.push(v);
            }
            else
                ch[o][i] = ch[fail[o]][i];
        }
    }
    int query(char *s) {
        int o = 0, ans = 0;
        for (int i = 0; s[i]; i++) {
            int c = s[i] - 'a';
            o = ch[o][c];
            int tmp = o;
            while (tmp) {
                ans += val[tmp];
                val[tmp] = 0;
                tmp = fail[tmp];
            }
        }
        return ans;
    }
} ac;
char s[maxn];
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%s", s);
            ac.insert(s);
        }
        ac.build();
        scanf("%s", s);
        printf("%d\n", ac.query(s));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙橘子猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值