当前位置: 首页 > news >正文

AC 自动机 洛谷P3808 P3796 P5357

洛谷P3808

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int ch[maxn][30], fa[maxn], End[maxn];
int cnt = 0 , n;
int get_num(char c){return c - 'a';}
void build(string s){
	int cur = 0, len = s.length();
	for(int i = 0; i < len; i++){
		int c = get_num(s[i]);
		if(!ch[cur][c]) ch[cur][c] = ++cnt;
		cur = ch[cur][c];
	}
	End[cur]++;
}
void get_fail(){
	queue<int> q;
	for(int i = 0; i < 26; i++)
		if(ch[0][i])q.push(ch[0][i]);		
	while(!q.empty()){
		int u = q.front();	q.pop();
		for(int i = 0; i < 26; i++){
			int &v = ch[u][i];
			if(v){
				fa[v] =ch[fa[u]][i];
				q.push(v);
			}else v = ch[fa[u]][i];
		}		
	}
}
int query(string s){
	int cur = 0, ans = 0, len = s.length();
	for(int i = 0; i < len; i++){
		int c = get_num(s[i]);
		cur = ch[cur][c];
		for(int j = cur; j && End[j] != -1; j = fa[j]){
			ans += End[j];	End[j] = -1;
		}	
	}
	return ans;
}
int main() { 
	string s;
	cin >> n;
	for(int i = 0; i < n; i++){cin >> s; build(s);}
	get_fail();		cin >> s;
	cout << query(s) << endl;	
    return 0;
}

洛谷P3796

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int ch[maxn][30], fa[maxn], End[maxn], ans[158], Maxs;
int cnt = 0 , n;
string str[158];
int get_num(char c){return c - 'a';}
void build(string s, int x){
	int cur = 0, len = s.length();
	for(int i = 0; i < len; i++){
		int c = get_num(s[i]);
		if(!ch[cur][c]) ch[cur][c] = ++cnt;
		cur = ch[cur][c];
	}
	End[cur] = x;
}
void get_fail(){
	queue<int> q;
	for(int i = 0; i < 26; i++)
		if(ch[0][i])q.push(ch[0][i]);		
	while(!q.empty()){
		int u = q.front();	q.pop();
		for(int i = 0; i < 26; i++){
			int &v = ch[u][i];
			if(v){
				fa[v] =ch[fa[u]][i];
				q.push(v);
			}else v = ch[fa[u]][i];
		}		
	}
}
void query(string s){
	int cur = 0, len = s.length();
	for(int i = 0; i < len; i++){
		int c = get_num(s[i]);
		cur = ch[cur][c];
		for(int j = cur; j; j = fa[j])
			if(End[j]){
				ans[End[j]]++; 
				Maxs = max(Maxs, ans[End[j]]);
		}
	}	
}
int main() {
	string s; 	
	while(cin >> n && n){		
		for(int i = 0; i <= cnt; i++){			
			for(int j = 0; j < 26; j++)ch[i][j] = 0;
			fa[i] = 0; 
			End[i] = 0;
		}
		memset(ans, 0, sizeof(ans));		
		Maxs = 0;
		cnt = 0;
		for(int i = 1; i <= n; i++){
			cin >> str[i]; build(str[i], i);
		}
		get_fail();		cin >> s;
		query(s);
		cout << Maxs << endl;	
		for(int i = 1; i <= n; i++)
			if(ans[i] == Maxs)cout << str[i] << endl;
	}	
    return 0;
}

luogu p5357

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 6, maxm = 2e5 + 6;
int ch[maxn][30], fa[maxn], End[maxn], ans[maxm], Map[maxn], In[maxn];
int cnt = 0 , n;
int pos[maxn];
int get_num(char c){return c - 'a';}
void build(string s, int x){
	int cur = 0, len = s.length();
	for(int i = 0; i < len; i++){
		int c = get_num(s[i]);
		if(!ch[cur][c]) ch[cur][c] = ++cnt;
		cur = ch[cur][c];
	}	
//	if(!End[cur])End[cur] = x;
//	Map[x] = End[cur];    
	pos[x] = cur;	//new
}
void get_fail(){
	queue<int> q;
	for(int i = 0; i < 26; i++)
		if(ch[0][i])q.push(ch[0][i]);		
	while(!q.empty()){
		int u = q.front();	q.pop();
		for(int i = 0; i < 26; i++){
			int &v = ch[u][i];
			if(v){
				fa[v] =ch[fa[u]][i];	
				In[ch[fa[u]][i]]++ ; //新加
				q.push(v);
			}else v = ch[fa[u]][i];
		}		
	}
}
void query(string s){
	int cur = 0, len = s.length();
	for(int i = 0; i < len; i++){
		int c = get_num(s[i]);
		cur = ch[cur][c];
//		for(int j = cur; j; j = fa[j])
//			if(End[j])ans[End[j]]++; 
		ans[cur]++; 	//替换						
	}	
}
void topu(){
	queue<int> q;
    for(int i=1;i<=cnt;i++)
    	if(In[i]==0)q.push(i);
    while(!q.empty()){
        int u=q.front();	q.pop();
        int v=fa[u];	In[v]--;
        	ans[v] += ans[u];
        if(In[v]==0)q.push(v);
    }
}
int main() {
	string s; 	
	cin >> n;		
	for(int i = 1; i <= n; i++){
		cin >> s; build(s, i);
	}
	get_fail();		cin >> s;
	query(s);	
	topu();
	for(int i = 1; i <= n; i++)
		cout << ans[pos[i]] << endl;	
    return 0;
}
http://www.dtcms.com/a/122577.html

相关文章:

  • 深度学习篇---LSTMFFTGCT
  • CSV文件读取文件表头字符串含ZWNBSP(零宽度空白字符)
  • Python第八章02:数据可视化Pyecharts包无法使用
  • 【scikit-learn基础】--『预处理』之 数据缩放
  • telophoto源码查看记录 二
  • jmeter插件安装
  • 蓝桥杯备考
  • 【问题排查】SQLite安装失败
  • 五、Linux的使用和操作(2)
  • clickhouse注入手法总结
  • 13.支持 RESTful
  • 请你说一说测试用例的边界
  • Redis的used_memory_peak_perc和used_memory_dataset_perc超过90%会怎么样
  • zsh: command not found: hdc - 鸿蒙 HarmonyOS Next
  • aws平台练习
  • 【VUE3】Eslint 与 Prettier 的配置
  • 使用Java操作Neo4j数据库
  • Kotlin 学习--数组
  • 【大模型微调】如何解决llamaFactory微调效果与vllm部署效果不一致如何解决
  • 深入了解提示工程:通往AI高效协作的桥梁
  • Redis和数据库一致性问题
  • 微前端架构深度解析
  • 华为海思IC前端中后端(COTXPU)岗位笔试机考题
  • 深入理解 rsync daemon 模式(守护进程)
  • 【简单理解什么是简单工厂、工厂方法与抽象工厂模式】
  • 【“星睿O6”AI PC开发套件评测】在O6开发板使用gemma-2b测试CPU性能
  • 测试用例 [软件测试 基础]
  • 加油站小程序实战教程10开通会员
  • 重构居家养老安全网:从 “被动响应” 到 “主动守护”
  • Windows上使用Qt搭建ARM开发环境