URAL-1989 Subpalindromes 多项式Hash+树状数组

  题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1989

  题意:给出一个字符串,m个操作:1,修改其中一个字符串,2,询问 [a, b] 是不是回文串。数据范围10^5。

  如何快速判断字符串是不是回文串,可以用到多项式Hash。假设一个串s,那么字串s[i, j]的Hash值就是H[i, j]=s[i]+s[i+1]*x+s[i+2]*(x^2)+...+s[j]*(x^(i-j))。由于只有小写字母,因此x取27。但是H[i, j]这会很大,我们取模就可了,可以把变量类型设为unsigned long long, 那么自动溢出就相当于模2^64了。对于不同串但是Hash相同的情况,这种情况的概率是非常小的,通常可以忽略,当然我们也可以对x取多次值,求出不同情况下的Hash值。然后我们就可以用线段树或者树状数组来维护这个和了,复杂度O(nlogn)。

  1 //STATUS:C++_AC_140MS_2777KB
  2 #include <functional>
  3 #include <algorithm>
  4 #include <iostream>
  5 //#include <ext/rope>
  6 #include <fstream>
  7 #include <sstream>
  8 #include <iomanip>
  9 #include <numeric>
 10 #include <cstring>
 11 #include <cassert>
 12 #include <cstdio>
 13 #include <string>
 14 #include <vector>
 15 #include <bitset>
 16 #include <queue>
 17 #include <stack>
 18 #include <cmath>
 19 #include <ctime>
 20 #include <list>
 21 #include <set>
 22 #include <map>
 23 using namespace std;
 24 //#pragma comment(linker,"/STACK:102400000,102400000")
 25 //using namespace __gnu_cxx;
 26 //define
 27 #define pii pair<int,int>
 28 #define mem(a,b) memset(a,b,sizeof(a))
 29 #define lson l,mid,rt<<1
 30 #define rson mid+1,r,rt<<1|1
 31 #define PI acos(-1.0)
 32 //typedef
 33 typedef __int64 LL;
 34 typedef unsigned __int64 ULL;
 35 //const
 36 const int N=100010;
 37 const int INF=0x3f3f3f3f;
 38 const int MOD=95041567,STA=8000010;
 39 const LL LNF=1LL<<60;
 40 const double EPS=1e-8;
 41 const double OO=1e15;
 42 const int dx[4]={-1,0,1,0};
 43 const int dy[4]={0,1,0,-1};
 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 45 //Daily Use ...
 46 inline int sign(double x){return (x>EPS)-(x<-EPS);}
 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
 50 template<class T> inline T Min(T a,T b){return a<b?a:b;}
 51 template<class T> inline T Max(T a,T b){return a>b?a:b;}
 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
 56 //End
 57 
 58 ULL bit[N],c[N][2];
 59 char s[N];
 60 int n,len;
 61 
 62 inline int lowbit(int x)
 63 {
 64     return x&(-x);
 65 }
 66 
 67 void update(int x,ULL val,int flag)
 68 {
 69     while(x<=len){
 70         c[x][flag]+=val;
 71         x+=lowbit(x);
 72     }
 73 }
 74 
 75 ULL sum(int x,int flag)
 76 {
 77     ULL ret=0;
 78     while(x){
 79         ret+=c[x][flag];
 80         x-=lowbit(x);
 81     }
 82     return ret;
 83 }
 84 
 85 int main()
 86 {
 87  //   freopen("in.txt","r",stdin);
 88     int i,j,w,L,R;
 89     char op[20],ch;
 90     bit[0]=1;
 91     for(i=1;i<N;i++)bit[i]=bit[i-1]*27;
 92     while(~scanf("%s",s))
 93     {
 94         len=strlen(s);
 95         mem(c,0);
 96         for(i=0;i<len;i++){
 97             update(i+1,(s[i]-'a'+1)*bit[i],0);
 98             update(i+1,(s[len-i-1]-'a'+1)*bit[i],1);
 99         }
100         scanf("%d",&n);
101         while(n--){
102             scanf("%s",op);
103             if(op[0]=='p'){
104                 scanf("%d%d",&L,&R);
105                 ULL a=(sum(R,0)-sum(L-1,0))*bit[len-R];
106                 ULL b=(sum(len-L+1,1)-sum(len-R,1))*bit[L-1];
107                 if(a==b)printf("Yes\n");
108                 else printf("No\n");
109             }
110             else {
111                 scanf("%d %c",&w,&ch);
112                 update(w,(ch-s[w-1])*bit[w-1],0);
113                 update(len-w+1,(ch-s[w-1])*bit[len-w],1);
114                 s[w-1]=ch;
115             }
116         }
117     }
118     return 0;
119 }

 

posted @ 2013-10-30 00:34  zhsl  阅读(940)  评论(0编辑  收藏  举报