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)。
//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=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//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[N],c[N][];
char s[N];
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=;
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[],ch;
bit[]=;
for(i=;i<N;i++)bit[i]=bit[i-]*;
while(~scanf("%s",s))
{
len=strlen(s);
mem(c,);
for(i=;i<len;i++){
update(i+,(s[i]-'a'+)*bit[i],);
update(i+,(s[len-i-]-'a'+)*bit[i],);
}
scanf("%d",&n);
while(n--){
scanf("%s",op);
if(op[]=='p'){
scanf("%d%d",&L,&R);
ULL a=(sum(R,)-sum(L-,))*bit[len-R];
ULL b=(sum(len-L+,)-sum(len-R,))*bit[L-];
if(a==b)printf("Yes\n");
else printf("No\n");
}
else {
scanf("%d %c",&w,&ch);
update(w,(ch-s[w-])*bit[w-],);
update(len-w+,(ch-s[w-])*bit[len-w],);
s[w-]=ch;
}
}
}
return ;
}
URAL-1989 Subpalindromes 多项式Hash+树状数组的更多相关文章
- URAL 1989 Subpalindromes (多项式hash) +【线段树】
<题目链接> <转载于 >>> > 题目大意:给你一段字符串,进行两种操作:1.询问[l,r]这个区间中的字符串是否是回文串: 2.更改该字符串中对应下标的 ...
- [bzoj2124]等差子序列(hash+树状数组)
我又来更博啦 2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 941 Solved: 348[Submit][Statu ...
- acdream1197 Points In Cuboid(hash树状数组)
题目链接:http://acdream.info/problem?pid=1197 题意:给出三维空间n个点,m个查询,每次查询某个立方体内的点的个数. 思路:按照一维排序,根据查询插入,其他两位用二 ...
- P7114 [NOIP2020] 字符串匹配 (字符串hash+树状数组)
好多题解用的扩展KMP(没学过,所以不用这种方法). 我们按照题目要求记F(s)表示s串的权值,可以预处理出前缀权值(用于A)和后缀权值(用于C),枚举AB的长度i=2~n-1,不需要分开枚举,我们只 ...
- CF452F Permutations/Luogu2757 等差子序列 树状数组、Hash
传送门--Luogu 传送门--Codeforces 如果存在长度\(>3\)的等差子序列,那么一定存在长度\(=3\)的等差子序列,所以我们只需要找长度为\(3\)的等差子序列.可以枚举等差子 ...
- ural 1989(树状数组+多项式hash)
题意:给出一个字符串.有两种操作,一个是p a b,问字符串从位置a到位置b的子串是否是一个回文子串.还有一个操作 c a b,把字符串位置a的字符替换为b. 题解:由于字符串长度为1e5且问的次数也 ...
- bzoj 2124 等差子序列 树状数组维护hash+回文串
等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 1919 Solved: 713[Submit][Status][Discuss] Desc ...
- 【BZOJ2124】等差子序列 树状数组维护hash值
[BZOJ2124]等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pLen<=N ...
- ACdreamoj 1011(树状数组维护字符串hash前缀和)
题目链接:http://acdream.info/problem? pid=1019 题意:两种操作,第一种将字符串某个位置的字符换为还有一个字符.另外一种查询某个连续子序列是否是回文串: 解法:有两 ...
随机推荐
- 上海二手房8月排名:链家、悟空找房、中原、太平洋、我爱我家、易居、房天下、iwjw、房多多、房好多、q房网、、、
房产网站总结 链家: 悟空找房: 中原: 太平洋: 我爱我家: 易居: 房天下: iwjw:有较多二手房信息 链家称王 房多多领跑电商平台 近日,云房数据公布了8月上海房产中介成交数据,从排行榜来看, ...
- [转]JavaScript 的性能优化:加载和执行
原文链接:http://www.ibm.com/developerworks/cn/web/1308_caiys_jsload/index.html?ca=drs- JavaScript 的性能优化: ...
- UVA 11419 SAM I AM(最大二分匹配&最小点覆盖:König定理)
题意:在方格图上打小怪,每次可以清除一整行或一整列的小怪,问最少的步数是多少,又应该在哪些位置操作(对输出顺序没有要求). 分析:最小覆盖问题 这是一种在方格图上建立的模型:令S集表示“行”,T集表示 ...
- WMware 10 Ubuntu 12.04 进入Unity模式
/********************************************************************* * WMware 10 Ubuntu 12.04 进入Un ...
- Android 编程下两种方式注册广播的区别
常驻型广播 常驻型广播,当你的应用程序关闭了,如果有广播信息来,你写的广播接收器同样的能接收到,它的注册方式就是在你应用程序的AndroidManifast.xml 中进行注册,这种注册方式通常又被称 ...
- PHP配置文件详解php.ini [转]
[PHP] ; PHP还是一个不断发展的工具,其功能还在不断地删减 ; 而php.ini的设置更改可以反映出相当的变化, ; 在使用新的PHP版本前,研究一下php.ini会有好处的 ;;;;;;;; ...
- == Rickard Oberg & TheServerSide
看了Hani Suleiman和Rickard Oberg ,发现其实每个所谓的权威都应该有被质疑的绝对,可能往往权威会令人觉得理所应当
- 广义线性模型 GLM
Logistic Regression 同 Liner Regression 均属于广义线性模型,Liner Regression 假设 $y|x ; \theta$ 服从 Gaussian 分布,而 ...
- UVa572 - Oil Deposits
解题思路:好久没写搜索了,练练手,陶冶情操.不多说,直接贴代码: #include<cstdio> #include<cstring> #include<algorith ...
- 多线程&多进程解析:Python、os、sys、Queue、multiprocessing、threading
当涉及到操作系统的时候,免不了要使用os模块,有时还要用到sys模块. 设计到并行程序,一般开单独的进程,而不是线程,原因是python解释器的全局解释器锁GIL(global interpreter ...