HDU4399:Query

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
  
  
Case 1: 2 1 0 1 4 1
 


//苦逼

看了很久才看懂

果然我还是太水了

先说 1 1 2 b

意思就是把 1 字符串的下标为 2 的字符改为 b

这样1的字符串就变为饿aabbba

然后2 0

就是两个字符串从0下标开始

算下连续有几个相同的字符

差不多就这样了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <memory.h>
using namespace std;
const int maxn = 1000007;
int flag[maxn<<2],sum[maxn<<2],t,cas = 1;
char s1[maxn],s2[maxn];
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
void pushUP(int rt)
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void build(int l,int r,int rt)
{
    if(l == r)
    {
        if(s1[l] == s2[l]) flag[l] = 1;
        else flag[l] = 0;
        sum[rt] = flag[l];
        return ;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    pushUP(rt);
}

void update(int op,int pos,char c,int l,int r,int rt)
{
    if(l == r)
    {
        if(op == 1) s1[l] = c;
        if(op == 2) s2[l] = c;
        if(s1[l] == s2[l]) flag [l] = 1;
        else flag[l] = 0;
        sum[rt] = flag[l];
        return ;
    }
    int m = (l+r)>>1;
    if(pos<=m) update(op,pos,c,lson);
    else update(op,pos,c,rson);
    pushUP(rt);
}

int query(int pos,int l,int r,int rt)
{
    if(l == r) return sum[rt];
    if(l<=pos && pos<=r)
    {
        if(sum[rt] == r-l+1)
        {
            return r-pos+1;
        }
        else
        {
            int m = (l+r)>>1,ans = 0;
            if(pos <= m)
            {
                ans+=query(pos,lson);
                if(ans == m-pos+1)
                {
                    ans+=query(m+1,rson);
                }
            }
            else ans+=query(pos,rson);
            return ans;
        }
    }
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",(s1+1),(s2+1));
        int l1 = strlen(s1+1);
        int l2 = strlen(s2+1);
        int n = max(l1,l2);
        build(1,n,1);
        int m,op,idx,pos;
        char c[2];
        scanf("%d",&m);
        printf("Case %d:\n",cas++);
        while(m--)
        {
            scanf("%d",&op);
            if(op == 2)
            {
                scanf("%d",&pos);
                printf("%d\n",query(pos+1,1,n,1));
            }
            else
            {
                scanf("%d%d%s",&idx,&pos,&c);
                update(idx,pos+1,c[0],1,n,1);
            }
        }
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值