hdu 4339 线段树

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
 Case 1:
  2
 1
 0
 1
 4
 1
  题意:
找出从i字符开始的最大连续字符长度.
1  a i c  表示把第a行的第i个字符替换为字符c
2  a     找出从a开始的两组字符串的最大连续字符长度
这里线段树区间用来保存两组字符串第一个不匹配字符的下标,然后转换成单点更新,求区间最值
AC code:

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int Init,a;
char str1[1000002],str2[1000002];
int min(int a,int b) { return a>b?b:a;}
struct segeTree
{
 int left,right;
 int first;        //首次出现不匹配的位置
}Tree[1000002*4];
void build(int left,int right,int k)
{
 if(left==right)
 {
  Tree[k].left=left;
  Tree[k].right=right;
  Tree[k].first=Init;
  return ;
 }
 else
 {
 int mid=(left+right)/2;
 Tree[k].left=left;
 Tree[k].right=right;
 Tree[k].first=Init;
 build(left,mid,2*k);
 build(mid+1,right,2*k+1);
    }
}

 

void update(int x,int change,int k)
{
 if(Tree[k].left==Tree[k].right&&Tree[k].left==x)
 {
  Tree[k].first=change;
  return ;
 }
 int mid=(Tree[k].left+Tree[k].right)/2;
 if(x<=mid)
 update(x,change,2*k);
 else if(x>mid)
 update(x,change,2*k+1);
 Tree[k].first=min(Tree[2*k].first,Tree[2*k+1].first);
}

int query(int index,int k)
{
 if(index<=Tree[k].left)
 return Tree[k].first;
 int mid=(Tree[k].left+Tree[k].right)/2;
 int d;
 if(index<=mid) 
 a=query(index,2*k);
 d=query(index,2*k+1);
 return min(a,d);
}
int main()
{
 int test,num,flag,index,ans,i,Count=0;
 scanf("%d",&test);
 while(test--)
 {
  Count++;
  memset(str1,0,sizeof(str1));
  memset(str2,0,sizeof(str2));
  memset(Tree,0,sizeof(Tree));
  scanf("%s%s",str1,str2);
  int m=min(strlen(str1),strlen(str2));
  Init=m+1;
  build(1,m,1);
  a=Init;
  for(i=0;i<m;++i)
  {
   if(str1[i]==str2[i]) update(i+1,Init,1);
   else if(str1[i]!=str2[i])            update(i+1,i+1,1);
  }
  scanf("%d",&num);
  printf("Case %d:\n",Count);
        while(num--)
        {
   scanf("%d",&flag);
   if(flag==2)
   {
    scanf("%d",&index);
    a=Init;
    ans=query(index+1,1)-(index+1);   //出现的位置减去自身的位置即可得出个数
    printf("%d\n",ans);
   }
   if(flag==1)
   {
    int x,y;
    char z;
    scanf("%d%d",&x,&y);
    cin>>z;
    if(x==1)
    {
    if(str1[y]==str2[y]&&str2[y]!=z) update(y+1,y+1,1);      //原先相等且替换之后不相等
    if(str1[y]!=str2[y]&&str2[y]==z) update(y+1,Init,1);     //原先不等但替换之后相等
    str1[y]=z;
       }
       if(x==2)
       {
    if(str2[y]==str1[y]&&str1[y]!=z) update(y+1,y+1,1);    //同上
    if(str2[y]!=str1[y]&&str1[y]==z) update(y+1,Init,1);
    str2[y]=z;
        }
   }
  }
 }
}
    
    
    
   
 
 

 
 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值