Time limit: 0.5 second
Memory limit: 64 MB
You have a string and queries of two types:
- replace i’th character of the string by character a;
- check if substring sj...sk is a palindrome.
Input
The first line contains a string consisting of n small English letters. The second line contains an integer m that is the number of queries (5 ≤ n, m ≤ 105). The next m lines contain the queries.
Each query has either form “change i a”, or “palindrome? j k”, where i, j, k are integers (1 ≤ i ≤ n; 1 ≤ j ≤ k ≤ n), and character a is a small English letter.
Output
To all second type queries, you should output “Yes” on a single line if substring sj...sk is a palindrome and “No” otherwise.
Sample
input | output |
---|---|
abcda 5 palindrome? 1 5 palindrome? 1 1 change 4 b palindrome? 1 5 palindrome? 2 4 | No Yes Yes Yes |
Problem Author: Mikhail Rubinchik
Problem Source: Open Ural FU Championship 2013
该题我用了字符串双哈希,然后用线段树维护一段区间的哈希值,如果双哈希都相同,则认为可行
#include<bits/stdc++.h>
#define ls l, mid, rt << 1
#define rs mid + 1, r, rt << 1 | 1
using namespace std;
const int MAXN = 100000 + 10;
char q[MAXN];
int Pow1[MAXN], Pow2[MAXN];
int n, m, k1 = 137, k2 = 173;
struct node
{
int l, r, len;
int KeyL1, KeyR1;
int KeyL2, KeyR2;
}tree[MAXN << 2];
void push_up(int L, int R, int rt)
{
tree[rt].KeyL1 = tree[rt << 1].KeyL1 + tree[rt << 1 | 1].KeyL1 * Pow1[L];
tree[rt].KeyL2 = tree[rt << 1].KeyL2 + tree[rt << 1 | 1].KeyL2 * Pow2[L];
tree[rt].KeyR1 = tree[rt << 1].KeyR1 * Pow1[R] + tree[rt << 1 | 1].KeyR1;
tree[rt].KeyR2 = tree[rt << 1].KeyR2 * Pow2[R] + tree[rt << 1 | 1].KeyR2;
}
void build(int l, int r, int rt)
{
tree[rt].l = l; tree[rt].r = r; tree[rt].len = r - l + 1;
tree[rt].KeyL1 = tree[rt].KeyR1 = 0;
tree[rt].KeyL2 = tree[rt].KeyR2 = 0;
if(l == r)
{
tree[rt].KeyL1 = tree[rt].KeyR1 = q[l];
tree[rt].KeyL2 = tree[rt].KeyR2 = q[l];
return;
}
int mid = (l + r) >> 1;
build(ls);
build(rs);
push_up(mid - l + 1, r - mid , rt);
}
void update(int a, char b[], int l, int r, int rt)
{
if(l == r)
{
tree[rt].KeyL1 = tree[rt].KeyR1 = b[0];
tree[rt].KeyL2 = tree[rt].KeyR2 = b[0];
return;
}
int mid = (l + r) >> 1;
if(mid >= a)
update(a, b, ls);
if(mid < a)
update(a, b, rs);
push_up(mid - l + 1, r - mid, rt);
}
node query(int x, int y, int l, int r, int rt)
{
if(l >= x && r <= y)
return tree[rt];
node ans, ans1, ans2;
int T = 0;
int mid = (l + r) >> 1;
if(mid >= x)
ans1 = query(x, y, ls),T += 1;
if(mid < y)
ans2 = query(x, y, rs),T += 2;
if(T == 1)
ans = ans1;
else if(T == 2)
ans = ans2;
else
{
ans.KeyL1 = ans1.KeyL1 + ans2.KeyL1 * Pow1[mid - max(x, l) + 1];
ans.KeyL2 = ans1.KeyL2 + ans2.KeyL2 * Pow2[mid - max(x, l) + 1];
ans.KeyR1 = ans1.KeyR1 * Pow1[min(y, r) - mid] + ans2.KeyR1;
ans.KeyR2 = ans1.KeyR2 * Pow2[min(y, r) - mid] + ans2.KeyR2;
}
return ans;
}
int main()
{
while(~scanf("%s", q + 1))
{
q[0] = '0'; n = strlen(q) - 1;
Pow1[0] = 1;
Pow2[0] = 1;
for(int i = 1; i <= n; i ++)
{
Pow1[i] = Pow1[i - 1] * k1;
Pow2[i] = Pow2[i - 1] * k2;
}
build(1, n, 1);
char op[20];
scanf("%d", &m);
for(int i = 0 ; i < m; i ++)
{
scanf("%s", op);
if(op[0] == 'c')
{
int a;char b[2];
scanf("%d %s", &a, b);
update(a, b, 1, n, 1);
}
else
{
int a, b;
scanf("%d %d", &a, &b);
node ans = query(a, b, 1, n, 1);
if(ans.KeyL1 == ans.KeyR1 && ans.KeyL2 == ans.KeyR2)
printf("Yes\n");
else
printf("No\n");
}
}
}
return 0;
}