建议加强数据
查看原帖
建议加强数据
188409
zhanglunhao楼主2020/7/24 01:56

这是AC代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<assert.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define INF 0x3f3f3f3f
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
const ll mod=1000000007;
const double PI = acos(-1.0);
const double epsilon = PI / 180.0;//角度转弧度 
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N = 5e5+10, M = 1e6+10;
int son[N][26], cnt[N], idx;
int q[N], ne[N];
char t[M], s[M];
void insert(char x[], int m) {
	int p = 0;
	rep(i, 1, m + 1) {
		int u = x[i] - 'a';
		if(!son[p][u]) son[p][u] = ++idx;
		p = son[p][u];
	}
	cnt[p]++;
}
void build() {
	int hh = 0, tt = -1;
	rep(i, 0, 26)
		if(son[0][i])
			q[++tt] = son[0][i];
	while(hh <= tt) {
		int now = q[hh++];
		rep(i, 0, 26) {
			int p = son[now][i];
			if(!p) son[now][i] = son[ne[now]][i];
			else {
				ne[p] = son[ne[now]][i];
				q[++tt] = p;
			}
		}
	}
}
int n;
int main() {
	cin>>n;
	rep(i, 0, n) {
		scanf(" %s", s + 1);
		insert(s, strlen(s + 1));
	}
	build();
	scanf(" %s", t + 1);
	int m = strlen(t + 1);
	int ans = 0;
	for(int i = 1, j = 0;i <= m;i++) {
		int u = t[i] - 'a';
		j = son[j][u];
		int p = j;
		while(p&&cnt[p]) {
			ans += cnt[p];
			cnt[p] = 0;
			p = ne[p];
		}
	}
	cout<<ans;
	return 0;
}

建议增加的hack样例

2
qabqks
ab
qabqksatffqp

正确答案

2
2020/7/24 01:56
加载中...