Castle
程序员文章站
2022-06-11 09:44:52
...
KMP
如果集合中有一个字符串
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1300000;
int sum[maxn], f[maxn], n, m, op, t;
char s[maxn], c;
bool p[maxn] = {0};
char get_char() {
char o = ' ';
while (!(o >= 'a' && o <= 'z')) o = getchar();
return o;
}
int get_int() {
char o = ' ';
while (!(o >= '0' && o <= '9')) o = getchar();
return o-'0';
}
int main() {
scanf("%d %d", &n, &m);
scanf("%s", s+1);
memset(f, 0, sizeof(f));
for (int i = 2; i <= n; i++) {
t = f[i-1];
while (t && s[i] != s[t+1]) t = f[t];
if (s[i] == s[t+1]) f[i] = t+1;
else f[i] = 0;
}
while (m--) {
op = get_int();
if (op == 1) {
c = get_char();
s[++n] = c;
t = f[n-1];
while (t && s[n] != s[t+1]) t = f[t];
if (s[n] == s[t+1]) {
sum[n] = sum[t+1];
f[n] = t+1;
} else f[n] = 0;
} else if (op == 2) {
if (!p[n]) {
p[n] = true;
sum[n]++;
}
} else {
printf("%d\n", sum[n]);
}
}
}
上一篇: 万兴神剪手中如何添加视频片段?使用万兴神剪手添加视频片段的方法
下一篇: 我踩过的那些坑
推荐阅读
-
Castle DynamicProxy基本用法(AOP)
-
使用MEF与Castle实现AOP
-
Castle Windsor 的动态代理类如何获取实际类型
-
Abp.Castle.Log4Net : Method 'get_IsTraceEnabled' does not have an implementation
-
使用Bouncy Castle(pom版本:bcprov-jdk15on 1.59)中SM3摘要算法
-
Castle
-
P1457 城堡 The Castle
-
POJ 1164 The Castle 深搜入门(城堡问题)
-
1250:The Castle
-
练习3 - The Castle(高端转化)