P3808求助AC自动机
  • 板块灌水区
  • 楼主Buried_Dream
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/12/29 19:58
  • 上次更新2023/10/28 13:23:51
查看原帖
P3808求助AC自动机
396974
Buried_Dream楼主2021/12/29 19:58

50分求助

这题太ex了,数据两个一个小的要死,跑KMP都能跑出来,另外一个大的要命。

求调!QAQ

/*
work by: TLE_Automation
Time: O(TLE)
knowledge:
*/
#include<bits/stdc++.h>
#define TLE std
//#define int long long
#define Min(x, y)  (x > y ? y : x);
#define Max(x, y)  (x > y ? x : y);
#define orz cout << "szt lps fjh AK IOI";
using namespace TLE;

const int maxn = 3e6;
const int MAXN = 3e3;
const double down = 0.996;
const double limit = 1e-10;

inline int read() {
	int s = 0, w = 1;
	char ch = getchar();
	while (!isdigit(ch)) {if(ch == '-') {w = -1;}ch = getchar();}
	while (isdigit(ch)) {s = (s << 1) + (s << 3) + (ch ^ 48);ch = getchar();}
	return s * w;
}

inline double Read() {
	int js = 1;
	double s = 0, m = 0;
	bool fh_1 = true, fh_2 = true;
	char ch = getchar();
	while(!isdigit(ch)) {if(ch == '-') {fh_1 = false;}ch = getchar();}
	while(isdigit(ch)) {s = s * 10.0 + (ch ^ 48);ch = getchar(); }
	while(!isdigit(ch)) {if(ch == '.') {fh_2 = false;}ch = getchar();}
	while(isdigit(ch)) {m = m * 10.0 + (ch ^ '0');js *= 10;ch = getchar();}
	if(fh_1 && fh_2) {return s;}
	else if(!fh_1 && fh_2) {return -s;}
	else if(fh_1 && !fh_2) {return s + (m * 1.0 / js);}
	else if(!fh_1 && !fh_2) {return -(s + (m * 1.0 / js));}
}

inline void print(int x) {
	if (x < 0 ) putchar('-'), x = -x;
	if (x > 9 ) print(x / 10);
	putchar(x % 10 + '0');
}

int cnt = 0;

struct node {
	int End, vis[26], fail;
}AC[maxn << 1];


void build(string s) {
	int now = 0, len = s.length();
	for(int i = 0; i < len; i++) {
		if(!AC[now].vis[s[i] - 'a']) AC[now].vis[s[i] - 'a'] = ++cnt; 
		now = AC[now].vis[s[i] - 'a'];
	}AC[now].End++; 
}

void Get_Fail() {
	queue <int> q;
	for(int i = 0; i < 26; i++) if(AC[0].vis[i]) AC[AC[0].vis[i]].fail = 0, q.push(AC[0].vis[i]);
	while(!q.empty()) {
		int u = q.front(); q.pop();
		for(int i = 0; i < 26; i++) {
			if(AC[u].vis[i]) {
				AC[AC[u].vis[i]].fail = AC[AC[u].fail].vis[i];
			}else {
				AC[u].vis[i] = AC[AC[u].fail].vis[i];
			}
		}
	} 
}

int AC_query(string s) {
	int now = 0, res = 0, len = s.length();
	for(int i = 0; i < len; i++) {
		now = AC[i].vis[s[i] - 'a'];
		for(int j = now; j && AC[j].End != -1; j = AC[j].fail) {
			res += AC[j].End, AC[j].End = -1;
		}
	}return res;	
}

string s, t;	

signed main() {
	int n = read();
	for(int i = 1; i <= n; i++) { cin >> s, build(s);	}
	AC[0].fail = 0, Get_Fail();
	cin >> s;
	return printf("%lld\n", AC_query(s)), 0;
}

2021/12/29 19:58
加载中...