Now we have a long long string, and we will have two kinds of operation on it.
C i y: change the ith letter to y;
Q i j: check whether the substring from ith letter to jth letter is a palindrome.
For each query operation, output "yes" if the corresponding substring is a palindrome, otherwise output "no".
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 #include<iostream> 6 using namespace std; 7 8 typedef unsigned long long LL; 9 const int N = 1000007; 10 11 LL bit[N], C[2][N]; 12 int n, m; 13 char str[N]; 14 void update( int x, LL val, int flag ) 15 { 16 for( ; x <= n; x += x&(-x) ) 17 C[flag][x] += val; 18 } 19 LL sum( int x, int flag ) 20 { 21 LL res = 0; 22 for( ; x >= 1; x -= x&(-x) ) 23 res += C[flag][x]; 24 return res; 25 } 26 void InitBit(){ 27 bit[0] = 1; 28 for(int i = 1; i < N; i++) 29 bit[i] = bit[i-1]*27; 30 } 31 void init(){ 32 n = strlen( str ); 33 memset( C, 0, sizeof(C) ); 34 for(int i = 1; i <= n; i++) 35 { 36 int j = (n+1)-i; 37 update( i, (str[i-1]-'a'+1)*bit[i-1], 0 ); 38 update( j, (str[i-1]-'a'+1)*bit[j-1], 1 ); 39 } 40 } 41 42 void solve(){ 43 scanf("%d", &m); 44 char op[2], ch[2]; 45 int a, b, c, d; 46 while( m-- ) 47 { 48 scanf("%s", op); 49 if( op[0] == 'Q' ) 50 { 51 scanf("%d%d",&a,&b); 52 if(a > b) swap(a,b); 53 d = (n+1)-a; c = (n+1)-b; 54 LL hash0 = ( sum(b,0)-sum(a-1,0) )*bit[c-1];//对阶 55 LL hash1 = ( sum(d,1)-sum(c-1,1) )*bit[a-1]; 56 puts( hash0 == hash1 ? "yes" : "no" ); 57 } 58 else 59 { 60 scanf("%d%s",&a,ch); 61 b = (n+1)-a; 62 update( a, (ch[0]-str[a-1])*bit[a-1], 0 ); 63 update( b, (ch[0]-str[a-1])*bit[b-1], 1 ); 64 str[a-1] = ch[0]; 65 } 66 } 67 } 68 int main(){ 69 InitBit(); 70 while( scanf("%s",str) != EOF) 71 { 72 init(); 73 solve(); 74 } 75 return 0; 76 }