Query
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1009 Accepted Submission(s): 302
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].
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.
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.
Sample Input
1
aaabba
aabbaa
7
2 0
2 1
2 2
2 3
1 1 2 b
2 0
2 3
Sample Output
Source
Recommend
zhoujiaqi2010
//len代表区间最左开始最长公共子串
//然后 if(len[k<<1]==m-l+1) len[k]=len[k<<1]+len[k<<1|1]; else len[k]=len[k<<1];
//开心的今早上1Y,本来打算昨晚写的、结果去看爱情公寓3了、、^_^
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
#define N 1000000
#define lson l,m,k<<1
#define rson m+1,r,k<<1|1
using namespace std;
char s1[N+2],s2[N+2];
int len[N<<2];
void up(int &k,int &l,int &m)
{
if(len[k<<1]==m-l+1)
len[k]=len[k<<1]+len[k<<1|1];
else
len[k]=len[k<<1];
}
void build(int l,int r,int k)
{
if(l==r)
{
if(s1[l]==s2[l])
len[k]=1;
else
len[k]=0;
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
up(k,l,m);
}
void update(int &i,int l,int r,int k)
{
if(l==r)
{
if(s1[l]==s2[l])
len[k]=1;
else
len[k]=0;
return;
}
int m=(l+r)>>1;
if(i<=m) update(i,lson);
else update(i,rson);
up(k,l,m);
}
int query(int &i,int l,int r,int k)
{
if(l==r)
{ return len[k];}
int m=(l+r)>>1,t;
if(i<=m)
{
t=query(i,lson);
if(t==m-i+1)
return t+len[k<<1|1];
return t;
}
return query(i,rson);
}
int main()
{
int l,l1,l2;
int t=1,T;
int q;
scanf("%d",&T);
int op,a,i;
char c;
while(T--)
{
scanf("%s",s1);
scanf("%s",s2);
l1=strlen(s1);
l2=strlen(s2);
l=l1<l2?l1:l2;//选取短的串建树
l--; //因为下标从0开始
build(0,l,1);
scanf("%d",&q);
printf("Case %d:\n",t++);
while(q--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d %d %c",&a,&i,&c);
if(i>l) continue;//超过长度更新没意义
if(a==1)s1[i]=c;
else s2[i]=c;
update(i,0,l,1);
}
else
{
scanf("%d",&i);
if(i>l){printf("0\n");continue;}//超长的话肯定是0
printf("%d\n",query(i,0,l,1));
}
}
}
return 0;
}