Gym - 100570E:Palindrome Query (hash+BIT+二分维护回文串长度)
题意:给定字符串char[],以及Q个操作,操作有三种:
1:pos,chr:把pos位置的字符改为chr
2:pos:问以pos为中心的回文串长度为多长。
3:pos:问以pos,pos+1为中心的回文串长度为多长。
思路:用hash表示一段字符串或者连续子串。我们用BIT记录hash前缀和,那么也可以用BIT修改前缀和。然后blabla,乱搞就行了。
当然为了保险,最好用双hash。
(此题暴力也可以过!
暴力代码:3962ms
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
char c[maxn],t;
int main()
{
int N,Q,opt,x;
scanf("%s%d",c+,&Q); N=strlen(c+);
while(Q--){
scanf("%d%d",&opt,&x);
if(opt==){
scanf(" %c",&t); c[x]=t;
}
else if(opt==){
int i=x,j=x;
while(i->=&&j+<=N&&c[i-]==c[j+]) i--,j++;
printf("%d\n",j-i+);
}
else {
if(c[x]!=c[x+]) { puts("-1"); continue;}
int i=x,j=x+;
while(i->=&&j+<=N&&c[i-]==c[j+]) i--,j++;
printf("%d\n",j-i+);
}
}
return ;
}
hash+BIT+二分:93ms(目前排第一?
#include<bits/stdc++.h>
#define uint unsigned int
using namespace std;
const int maxn=;
const uint seed=;
char c[maxn]; uint sum[maxn][],p[maxn]; int N;
uint query(int x,int opt){
uint res=; while(x) {
res+=sum[x][opt]; x-=(-x)&x;
} return res;
}
bool check(int L,int R)
{
if(L<||R>N) return false;
uint h1=(query(R,)-query(L-,))*p[L-];
uint h2=(query(R,)-query(L-,))*p[N-R];
if(h1==h2) return true; return false;
}
int main()
{
int Q,i,j;
scanf("%s%d",c+,&Q); N=strlen(c+);
p[]=;
for(i=;i<=N;i++) p[i]=p[i-]*seed;
for(i=;i<=N;i++){
for(j=i;j<=N;j+=(-j)&j) sum[j][]+=p[N-i]*c[i];
for(j=i;j<=N;j+=(-j)&j) sum[j][]+=p[i-]*c[i];
}
int opt,x,L,R,Mid,ans; char chr;
while(Q--){
scanf("%d%d",&opt,&x);
if(opt==){
scanf(" %c",&chr);
for(j=x;j<=N;j+=(-j)&j) sum[j][]+=p[N-x]*(chr-c[x]);
for(j=x;j<=N;j+=(-j)&j) sum[j][]+=p[x-]*(chr-c[x]);
c[x]=chr;
}
else if(opt==){
L=; R=N; ans=;
while(L<=R){
Mid=(L+R)>>;
if(check(x-Mid,x+Mid)) ans=Mid,L=Mid+;
else R=Mid-;
}
printf("%d\n",ans*+);
}
else{
L=; R=N; ans=-;
while(L<=R){
Mid=(L+R)>>;
if(check(x-Mid,x++Mid)) ans=Mid,L=Mid+;
else R=Mid-;
}
printf("%d\n",ans==-?-:ans*+);
}
}
return ;
}
Gym - 100570E:Palindrome Query (hash+BIT+二分维护回文串长度)的更多相关文章
- Gym 100570E : Palindrome Query
De Prezer loves palindrome strings. A string s1s2...sn is palindrome if and only if it is equal to i ...
- leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- Extend to Palindrome UVA - 11475(补成回文串)
题意: 就是用最少的字符把原字符串补成回文串 解析: emm/.../网上都是用kmp和后缀数组做的 我没想到这俩的思路...emmm... 想到了exkmp的 就是原串和逆串匹配一下 注意要保证 ...
- [CSP-S模拟测试]:回文串(hash+二分)
题目描述 $ASDFZ$的机房中不仅有红太阳,还有蓝太阳和原谅色太阳.有一天,太阳们来到机房,发现桌上有不知道哪个蒟蒻放上的问题:令$F(A,B)$表示选择一个串$A$的非空前缀$S$和串$B$的非空 ...
- LOJ 2452 对称 Antisymmetry——用hash求回文串数
概念 用hash求最长回文串/回文串数 首先,易知,回文串具有单调性. 如果字符串 $s[l...r]$ 为回文串串,那么 $s[x...y]$($l < x, y < r$ 且 $|l- ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- bzoj 2124 等差子序列 树状数组维护hash+回文串
等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 1919 Solved: 713[Submit][Status][Discuss] Desc ...
- BZOJ4755 JSOI2016扭动的回文串(二分答案+哈希)
显然答案应该是由单串以某位置为中心的极长回文串继续在另一个串里拓展得到的.枚举中间位置二分答案,哈希判断即可.注意考虑清楚怎么处理偶回文,比如像manacher一样加分隔符. #include< ...
- CF452F等差子序列 & 线段树+hash查询区间是否为回文串
记录一下一个新学的线段树基础trick(真就小学生trick呗) 给你一个1到n的排列,你需要判断该排列内部是否存在一个3个元素的子序列(可以不连续),使得这个子序列是等差序列.\(n\) <= ...
随机推荐
- (一)unity4.6Ugui中文教程文档-------概要
大家好,我是孙广东. 转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unityma ...
- RMQ with Shifts(线段树)
RMQ with Shifts Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Pra ...
- json-lib-2.5-jdk.jar 需要依赖的jar包
commons-lang3-3.1.jar commons-lang-2.5.jar ezmorph-1.0.6.jar commons-collections-3.2.1.jar commons-b ...
- Django 之 admin组件使用&源码解析
admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.可以在项目的 settings.py 中的 INSTALLED ...
- Linux安装过程记录信息
全新的linux安装完成后,会在root目录下有一下三个文件,记录了Linux的安装细节 anaconda-ks.cfg 以Kickstart配置文件的格式记录安装过程中设置的选项信息 install ...
- Python锁
# coding:utf-8 import threading import time def test_xc(): f = open("test.txt","a&quo ...
- 【leetcode刷题笔记】Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- CSS3分享按钮动画特效
在线演示 本地下载
- ll指令输出解析
从左到右: 文件属性 共有10-11位,分别表示: 1.表示目标属性,d目录,l连结文件,-文件 2-4,owner权限,顺序为: r可读.w可写.x可执行,例如: rwx可读写执行,rw-可读写不能 ...
- Cache-Control常用类型
可缓存性 no-cache 强制所有缓存了此响应的缓存用户,在使用已存储的缓存数据前,发送条件请求到原始服务器,若未过期,则使用缓存数据,否则重新获取 no-store 不存储有关客户端请求或服务器响 ...