Period
[Link](Problem - D - Codeforces)
题意
给定一个只含小写字母的字符串,每次询问将某个位置修改成#,问周期数量(独立修改)。
题解
假设串长为 n n n,假设#出现在 x x x位置,因为#是唯一的要想保证周期性,那么 T > n − x & & T < n T> n - x \&\& T<n T>n−x&&T<n。就等价于找新串中有多少个后缀等于前缀。我们可以kmp/hash出来以每个位置为结尾的前缀和对应的后缀是否相等,然后做一个前缀和即可,当x位于串的一般以前答案应该是 r e s [ x − 1 ] res[x-1] res[x−1],当答案位于一半以后等价于找x以后有多少个后缀和对应的前缀相等,就等价与找 n − x n-x n−x之前有多少个前缀和后缀相等答案应该是 r e s [ n − x ] res[n-x] res[n−x]。
Code
KMP
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e6 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int n, m, k;
char str[N];
int ne[N], mx[N];
int main() {
scanf("%s", str + 1);
n = strlen(str + 1);
for (int i = 2, j = 0; i <= n; i ++ ) {
while (j && str[i] != str[j + 1]) j = ne[j];
if (str[i] == str[j + 1]) j ++;
ne[i] = j;
}
for (int i = ne[n]; i; i = ne[i]) mx[i] ++;
for (int i = 2; i <= n; i ++ ) mx[i] += mx[i - 1];
int T; scanf("%d", &T);
while (T -- ) {
int x; scanf("%d", &x);
printf("%d\n", mx[min(x - 1, n - x)]);
}
return 0;
}
hash
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e6 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int n, m, k;
char str[N];
ULL h[N], p[N];
int res[N];
ULL get(int l, int r) {
return h[r] - h[l - 1] * p[r - l + 1];
}
int main() {
scanf("%s", str + 1); n = strlen(str + 1);
scanf("%d", &m);
p[0] = 1;
for (int i = 1; i <= n; i ++ ) {
h[i] = h[i - 1] * 131 + (str[i] - 'a' + 1);
p[i] = p[i - 1] * 131;
}
for (int i = 1; i < n; i ++ ) res[i] = res[i - 1] + (get(1, i) == get(n - i + 1,n));
while (m --) {
int x; scanf("%d", &x);
printf("%d\n", res[min(x - 1, n - x)]);
}
return 0;
}
这篇博客主要探讨了两种字符串处理算法——KMP和哈希,在解决特定问题时的应用。问题涉及在给定字符串中修改特定位置并计算周期数量。KMP算法通过构建部分匹配表找到相同前缀和后缀,而哈希方法通过计算字符串的哈希值来判断前后缀是否相等。文章提供了详细的代码实现,并给出了样例输入输出,适合对字符串算法感兴趣的读者深入理解。
1300

被折叠的 条评论
为什么被折叠?



