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 题意:两种操作,第一种将字符串某个位置的字符换为还有一个字符.另外一种查询某个连续子序列是否是回文串: 解法:有两 ...
随机推荐
- python之for学习
for i in range(100,-1,-1): print "%s\n"%i; import os path = 'D:\\Test' for root, ...
- [C]判断一个文件是否是jpg格式
同学要帮忙写的,用opencv的imread打开文件看抛出的异常来判断这种抖机灵的姿势就不写了… 首先知道jpg文件是以0xFFD8开始,以0xFFD9结尾的.所以直接拿来fseek fread,异或 ...
- hdu4003Find Metal Mineral(树形DP)
4003 思维啊 dp[i][j]表示当前I节点停留了j个机器人 那么它与父亲的关系就有了 那条边就走了j遍 dp[i][j] = min(dp[i][j],dp[child][g]+dp[i][j- ...
- List排序的两种简便方式
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...
- HDU 2870 Largest Submatrix
这三道题的关系是这样的,1505是1506的加强版,2870又是1505的加强版 如果按照上面由简到易的顺序来做的话,还是很简单的 这道题的思想就是 枚举+DP 因为某些字符可以变值,所以我们枚举a, ...
- InputStream重用技巧(利用ByteArrayOutputStream)
有时候我们需要对同一个InputStream对象使用多次.比如,客户端从服务器获取数据 ,利用HttpURLConnection的getInputStream()方法获得Stream对象,这时既要把数 ...
- Java调优之jvm和线程的内存分析
本文来源于铁木箱子的博客http://www.mzone.cc 这几天因为自己开发的一个网站在768M内存的机器上撑不起100多个用户的运行,因为每个用户启用功能后,系统将为每个用户分配8个左右的独立 ...
- ORACLE RAC 下非缺省端口监听配置(listener.ora tnsnames.ora)
不论是单实例还是RAC,对于非缺省端口下(1521)的监听器,pmon进程不会将service/instance注册到监听器,即不会实现动态注册.与单实例相同,RAC非缺省端口的监听器也是通过设置参数 ...
- MVC-READ5(asp.net web from PK asp.net MVC)
webform: ViewState问题 页面生命周期 不能很好的分解关注点 对HTML操控受限 抽象不完全 可测试性弱
- REST构架风格介绍:状态表述转移
REST(Representational State Transfer)是HTTP协议的作者Roy Fielding博士在其博士论文中提出的一种互联网应用构架风格.与以远程对象为核心的ORB和以服务 ...