hdu4339 Query
2024-10-10 06:19:48
Problem Description
You are given two strings s1[0..l1], s2[0..l2] and Q - number of queries.
Your task is to answer next queries:
1) 1 a i c - you should set i-th character in a-th string to c;
2) 2 i - you should output the greatest j such that for all k (i<=k and k<i+j) s1[k] equals s2[k].
Your task is to answer next queries:
1) 1 a i c - you should set i-th character in a-th string to c;
2) 2 i - you should output the greatest j such that for all k (i<=k and k<i+j) s1[k] equals s2[k].
Input
The first line contains T - number of test cases (T<=25).
Next T blocks contain each test.
The first line of test contains s1.
The second line of test contains s2.
The third line of test contains Q.
Next Q lines of test contain each query:
1) 1 a i c (a is 1 or 2, 0<=i, i<length of a-th string, 'a'<=c, c<='z')
2) 2 i (0<=i, i<l1, i<l2)
All characters in strings are from 'a'..'z' (lowercase latin letters).
Q <= 100000.
l1, l2 <= 1000000.
Next T blocks contain each test.
The first line of test contains s1.
The second line of test contains s2.
The third line of test contains Q.
Next Q lines of test contain each query:
1) 1 a i c (a is 1 or 2, 0<=i, i<length of a-th string, 'a'<=c, c<='z')
2) 2 i (0<=i, i<l1, i<l2)
All characters in strings are from 'a'..'z' (lowercase latin letters).
Q <= 100000.
l1, l2 <= 1000000.
Output
For each test output "Case t:" in a single line, where t is number of test (numbered from 1 to T).
Then for each query "2 i" output in single line one integer j.
Then for each query "2 i" output in single line one integer j.
Sample Input
1
aaabba
aabbaa
7
2 0
2 1
2 2
2 3
1 1 2 b
2 0
2 3
aaabba
aabbaa
7
2 0
2 1
2 2
2 3
1 1 2 b
2 0
2 3
Sample Output
Case 1:
2
1
0
1
4
1
2
1
0
1
4
1
这题属于区间合并,维护线段的llen(线段从左端点开始向右的最长连续1的长度),rlen(线段从右端点开始向左的最长连续1的长度),tlen(线段中最长连续1的长度,记录这个主要是为了剪枝,减少时间)。一开始先初始化,两个字符串,相同的部分记为1,不同的记为0,注意两个字符串的长度可能不同。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 1000100
char s1[maxn],s2[maxn];
int a[maxn],num;
struct node{
int l,r,llen,rlen,tlen;
}b[4*maxn];
void pushup(int i)
{
b[i].tlen=max(b[i*2].tlen,b[i*2+1].tlen);
b[i].tlen=max(b[i].tlen,b[i*2].rlen+b[i*2+1].llen);
b[i].llen=b[i*2].llen;b[i].rlen=b[i*2+1].rlen;
if(b[i*2].llen==b[i*2].r-b[i*2].l+1)b[i].llen+=b[i*2+1].llen;
if(b[i*2+1].rlen==b[i*2+1].r-b[i*2+1].l+1)b[i].rlen+=b[i*2].rlen;
}
void build(int l,int r,int i)
{
int mid;
b[i].l=l;b[i].r=r;
if(l==r){
b[i].tlen=b[i].llen=b[i].rlen=a[l];return;
}
mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
pushup(i);
}
void update(int id,int value,int i)
{
int mid;
if(b[i].l==b[i].r){
b[i].tlen=b[i].llen=b[i].rlen=value;return;
}
mid=(b[i].l+b[i].r)/2;
if(mid>=id)update(id,value,i*2);
else update(id,value,i*2+1);
pushup(i);
}
void question(int id,int i)
{
int mid;
if(b[i].l==b[i].r){
num=1;return;
}
if(b[i].tlen==b[i].r-b[i].l+1){
num=b[i].r-id+1;return;
}
mid=(b[i].l+b[i].r)/2;
if(mid>=id){ //用4个剪枝试一试
if(b[i*2].r-b[i*2].rlen+1<=id){
num=b[i*2].r-id+1+b[i*2+1].llen;return;
}
else{
question(id,i*2);
}
}
else{
if(b[i*2+1].l+b[i*2+1].llen-1>=id){
num=b[i*2+1].l+b[i*2+1].llen-1-id+1;return;
}
else question(id,i*2+1);
}
}
int main()
{
int n,m,i,j,T,len1,len2,len,d,e,flag,c,num1=0;
char f[10];
scanf("%d",&T);
while(T--)
{
num1++;
printf("Case %d:\n",num1);
scanf("%s%s",s1+1,s2+1);
len1=strlen(s1+1);len2=strlen(s2+1);
len=min(len1,len2);
for(i=1;i<=len;i++){
if(s1[i]==s2[i])a[i]=1;
else a[i]=0;
}
build(1,len,1);
scanf("%d",&m);
for(i=1;i<=m;i++){
scanf("%d",&c);
if(c==1){
scanf("%d%d%s",&d,&e,f);
e++;
if(e>len)continue;
if(s1[e]==s2[e])flag=1;
else flag=0;
if(d==1)s1[e]=f[0];
else s2[e]=f[0];
if(s1[e]==s2[e] && flag==0)update(e,1,1);
else if(s1[e]!=s2[e] && flag==1)update(e,0,1); //这里可以节省200ms
}
else if(c==2){
scanf("%d",&d);
d++;
if(d>len || s1[d]!=s2[d]){
printf("0\n");continue;
}
num=0;
question(d,1);
printf("%d\n",num);
}
}
}
}
最新文章
- 2. 上传Android代码到github
- [NOIP2011] 选择客栈
- GBDT基本理论及利用GBDT组合特征的具体方法(收集的资料)
- Entity Framework后台采用分页方式取数据与AspNetPager控件的使用
- JQuery:JQuery捕获HTML
- ajax连接池和XMLHttpRequest
- 黑马程序员——Foundation之NSString和NSMutableString
- synchronize学习
- English - consist of 和 compose of 的区别
- es6--(三)set和map数据结构
- linux logrotate配置
- code_smith生成实体类
- yaml在python中的应用简单整理
- PKI(公钥基础设施)基础知识笔记
- Android中如何使用xmlns
- 基于HTML5 Canvas 实现地铁站监控
- 如何解析android访问webservice返回的SoapObject数据(可用)
- 进阶:2.GBDT算法梳理
- 机器学习—集成学习(Adaboost)
- 站点CSS样式不起作用,或仅仅有一部分起作用?随手记
热门文章
- 【Oracle】用sqlplus登录的各种方式
- ctfhub技能树—web前置技能—http协议—请求方式
- 工作记录:记一次线上ZK掉线问题排查
- SAPLink 非常好用的工具
- Dubbo的设计理念原来就藏在这三张图中
- mysql 1449 : The user specified as a definer (&#39;usertest&#39;@&#39;%&#39;) does not exist 解决方法 (grant 授予权限)
- 写给 Linux 初学者的一封信
- docker mysql 设置忽略大小写
- MySQL库和表的操作
- Flask源码关于local的实现