KMP
#include<bits/stdc++.h>
using namespace std;
int t, n, m, nxt[200006];
char s[100006], p[200006];
inline void kmp() {
n = strlen(s + 1);
m = strlen(p + 1);
p[m + 1] = '#';
for (int i = 1, j = m + 2; i <= n; i++, j++)
p[j] = s[i];
int j = 0;
nxt[1] = 0;
for (int i = 2; i <= n + m + 1; i++) {
while (j && p[i] != p[j+1])
j = nxt[j];
if (p[j + 1] == p[i])
++j;
nxt[i] = j;
}
int ans = 0;
for (int i = m + 2; i <= n + m + 1; i++)
if (nxt[i] == m)
++ans;
if (!ans)
printf("-1\n-1\n");
else {
printf("%d\n", ans);
for (int i = m + 2; i <= n + m + 1; i++)
if (nxt[i] == m)
printf("%d ", i - 2 * m);
printf("\n");
}
}
int main(){
scanf("%d", &t);
while(t--) {
scanf("%s%s", s+1, p+1);
kmp();
}
return 0;
}
exkmp
#include <bits/stdc++.h>
using namespace std;
int t, n, m, z[200003];
char s[100003], p[200003];
inline void exkmp() {
n = strlen(s + 1);
m = strlen(p + 1);
p[m + 1] = '#';
for(int i = m + 2, j = 1; j <= n; i++, j++)
p[i] = s[j];
int L = 1, R = 0;
z[1] = 0;
for(int i = 2; i <= n + m + 1; i++) {
if(i > R)
z[i] = 0;
else{
int k = i - L + 1;
z[i] = min(z[k], R - i + 1);
}
while(i + z[i] <= n + m + 1 && p[z[i] + 1] == p[i + z[i]])
++z[i];
if(i + z[i] - 1 > R)
L = i, R = i + z[i] - 1;
}
int ans = 0;
for(int i = 2; i <= n + m + 1; i++)
if(z[i] == m)
++ans;
if(!ans)
printf("-1\n-1\n");
else{
printf("%d\n", ans);
for(int i = m + 2; i <= n + m + 1; i++)
if(z[i] == m)
printf("%d ", i - m - 1);
printf("\n");
}
}
int main() {
scanf("%d", &t);
while(t--) {
scanf("%s%s", s + 1, p + 1);
exkmp();
}
return 0;
}
manacher
#include <bits/stdc++.h>
using namespace std;
int n, m, p[200003];
char s[100003], t[200003];
inline void manacher() {
n = strlen(s + 1);
m = 0;
t[++m] = '$';
for(int i = 1; i <= n; i++)
t[++m] = s[i], t[++m] = '$';
int M = 0, R = 0;
for(int i = 1; i <= m; i++){
if(i > R)
p[i] = 1;
else
p[i] = min(p[M * 2 - i], R - i + 1);
while(i - p[i] > 0 && i + p[i] <= m && t[i - p[i]] == t[i + p[i]])
++p[i];
if(i + p[i] - 1 > R)
M = i, R = i + p[i] - 1;
}
int ans = 0;
for(int i = 1; i <= m; i++)
ans = max(ans, p[i]);
printf("%d\n", ans - 1);
}
int main() {
scanf("%s", s + 1);
manacher();
return 0;
}
字典树
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt=0;
char str[13];
int nxt[500005][30],tol[500005];
int search(int len){
int now=0;
int ok=1;
for(int i=0;i<len&&ok;i++){
int x=str[i]-'a';
if(!nxt[now][x])
ok=false;
now=nxt[now][x];
}
if(ok)
return tol[now];
else
return 0;
}
void insert(int len){
int now=0;
for(int i=0;i<len;i++){
int x=str[i]-'a';
if(!nxt[now][x])
nxt[now][x]=++cnt;
now=nxt[now][x];
++tol[now];
}
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",str);
int len=strlen(str);
insert(len);
}
scanf("%d",&m);
while(m--){
scanf("%s",str);
int len=strlen(str);
int ans=search(len);
printf("%d\n",ans);
}
return 0;
}
AC自动机
#include <bits/stdc++.h>
using namespace std;
int n, m, ans = 0, cnt = 0;;
int ne[1000001], c[1000001], nxt[1000001][27];
char s[1000002];
inline void insert() {
int now = 0;
int len = strlen(s);
for (int i = 0; i < len; i++) {
int x = s[i] - 'a' + 1;
if (!nxt[now][x])
nxt[now][x] = ++cnt;
now = nxt[now][x];
}
c[now] += 1;
}
inline void buildtree() {
queue<int> q;
for (int i = 1; i <= 26; i++)
if (nxt[0][i])
q.push(nxt[0][i]);
while (q.size()) {
int x = q.front();
q.pop();
for (int i = 1; i <= 26; i++) {
int y = nxt[x][i];
if (y)
ne[y] = nxt[ne[x]][i], q.push(y);
else
nxt[x][i] = nxt[ne[x]][i];
}
}
}
inline void auto_AC() {
int len = strlen(s);
int now = 0;
for (int i = 0; i < len; i++) {
int x = s[i] - 'a' + 1;
now = nxt[now][x];
for (int j = now; j && ~c[j]; j = ne[j])
ans += c[j], c[j] = -1;
}
printf("%d\n", ans);
}
int main() {
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%s", s);
insert();
}
buildtree();
scanf("%s", s);
auto_AC();
return 0;
}
AC自动机加强版
#include <bits/stdc++.h>
using namespace std;
struct node {
int num, idx;
bool operator < (const node &A) const {
if (num == A.num)
return idx < A.idx;
return num > A.num;
}
} q[151];
int n, m, ans = 0, cnt = 0, a[1000001];
int ne[1000001], c[1000001], nxt[1000001][27];
char s[1000002];
char str[152][1000002];
inline void insert(int k, char s[]) {
int now = 0;
int len = strlen(s);
for (int i = 0; i < len; i++) {
int x = s[i] - 'a' + 1;
if (!nxt[now][x])
nxt[now][x] = ++cnt;
now = nxt[now][x];
}
c[now] += 1;
a[now] = k;
}
inline void buildtree() {
queue<int> q;
for (int i = 1; i <= 26; i++)
if (nxt[0][i])
q.push(nxt[0][i]);
while (q.size()) {
int x = q.front();
q.pop();
for (int i = 1; i <= 26; i++) {
int y = nxt[x][i];
if (y)
ne[y] = nxt[ne[x]][i], q.push(y);
else
nxt[x][i] = nxt[ne[x]][i];
}
}
}
inline void auto_AC() {
int len = strlen(s);
int now = 0;
map<string, int> l = {};
for (int i = 0; i < len; i++) {
int x = s[i] - 'a' + 1;
now = nxt[now][x];
for (int j = now; j; j = ne[j])
q[a[j]].num += c[j];
}
}
inline void init() {
ans = 0, cnt = 0;
memset(a, {}, sizeof(a));
memset(ne, 0, sizeof(ne));
memset(c, 0, sizeof(c));
memset(nxt, 0, sizeof(nxt));
}
int main() {
while (1) {
scanf("%d", &m);
if (!m)
break;
init();
for (int i = 1; i <= m; i++) {
q[i].num = 0, q[i].idx = i;
scanf("%s", str[i]);
insert(i, str[i]);
}
buildtree();
scanf("%s", s);
auto_AC();
sort(q + 1, q + m + 1);
int mx = q[1].num;
printf("%d\n", mx);
for (int i = 1; i <= m; i++) {
if (q[i].num == mx)
printf("%s\n", str[q[i].idx]);
else
break;
}
}
return 0;
}