您的位置:首页 > 其它

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

2013-10-30 00:34 316 查看
  题目链接: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)。

//STATUS:C++_AC_140MS_2777KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=100010;
const int INF=0x3f3f3f3f;
const int MOD=95041567,STA=8000010;
const LL LNF=1LL<<60;
const double EPS=1e-8;
const double OO=1e15;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End

ULL bit
,c
[2];
char s
;
int n,len;

inline int lowbit(int x)
{
return x&(-x);
}

void update(int x,ULL val,int flag)
{
while(x<=len){
c[x][flag]+=val;
x+=lowbit(x);
}
}

ULL sum(int x,int flag)
{
ULL ret=0;
while(x){
ret+=c[x][flag];
x-=lowbit(x);
}
return ret;
}

int main()
{
//   freopen("in.txt","r",stdin);
int i,j,w,L,R;
char op[20],ch;
bit[0]=1;
for(i=1;i<N;i++)bit[i]=bit[i-1]*27;
while(~scanf("%s",s))
{
len=strlen(s);
mem(c,0);
for(i=0;i<len;i++){
update(i+1,(s[i]-'a'+1)*bit[i],0);
update(i+1,(s[len-i-1]-'a'+1)*bit[i],1);
}
scanf("%d",&n);
while(n--){
scanf("%s",op);
if(op[0]=='p'){
scanf("%d%d",&L,&R);
ULL a=(sum(R,0)-sum(L-1,0))*bit[len-R];
ULL b=(sum(len-L+1,1)-sum(len-R,1))*bit[L-1];
if(a==b)printf("Yes\n");
else printf("No\n");
}
else {
scanf("%d %c",&w,&ch);
update(w,(ch-s[w-1])*bit[w-1],0);
update(len-w+1,(ch-s[w-1])*bit[len-w],1);
s[w-1]=ch;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: