社区讨论

萌新刚学OI (x,请问......

P3808AC 自动机(简单版)参与者 8已保存回复 26

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
26 条
当前快照
1 份
快照标识符
@mi7w04vw
此快照首次捕获于
2025/11/21 04:31
4 个月前
此快照最后确认于
2025/11/21 06:47
4 个月前
查看原帖
这题卡 SAM\text{SAM} 空间没关系,毕竟是 AC\text{AC} 自动机的模板题
可是第一个测试点数据的字符集大小只有 11 ,也就是说 SAM\text{SAM} 的节点个数只有 10000011000001 ,所以如果只看第一个测试点那么下面这份代码的数组是开够了的
但是让我搞不懂的是为什么 MLE\text{MLE} 了啊......关键是把第一个点的数据下载下来在本地测一点问题也没有......
如下图,本地测试时内存峰值只有大约 101MB\text{101MB} ,但是在洛谷上就是 MLE\text{MLE} 了QAQ
screenshot
希望有神仙能告诉蒟蒻为什么QAQ,不胜感激
CPP
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <iostream>
#include <string>
using namespace std;

const int maxn=1000005;

int n;
int Ans;
string sr;
string ss[maxn];

struct SAM{
	int tot;
	int last;
	int len[maxn<<1];
	int prt[maxn<<1];
	unordered_map<char,int> ch[maxn];
	inline SAM(){
		tot=last=1;
	}
	inline void Insert(int x){
		int p=last,np=++tot;
		len[last=np]=len[p]+1;
		while(p&&(!ch[p][x]))
			ch[p][x]=np,p=prt[p];
		if(!p) prt[np]=1;
		else{
			int q=ch[p][x];
			if(len[q]==len[p]+1) prt[np]=q;
			else{
				int nq=++tot;
				len[nq]=len[p]+1;ch[nq]=ch[q];
				prt[nq]=prt[q];prt[q]=prt[np]=nq;
				while(p&&ch[p][x]==q)
					ch[p][x]=nq,p=prt[p];
			}
		}
	}
	inline bool Query(const string& ss){
		for(int i=0,p=1;i<ss.size();++i){
			char x=ss[i]-'a';
			if(!ch[p][x]) return 0;
			p=ch[p][x];
		}
		return 1;
	}
}M;

void Work();

int main(){
	Work();
	return 0;
}

void Work(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)
		cin>>ss[i];
	cin>>sr;
	for(int i=0;i<sr.size();++i)
		M.Insert(sr[i]-'a');
	for(int i=1;i<=n;++i)
		Ans+=M.Query(ss[i]);
	cout<<Ans<<endl;
	while(1);
}

回复

26 条回复,欢迎继续交流。

正在加载回复...