hdu 4339 multiset 或 线段树

http://acm.hdu.edu.cn/showproblem.php?pid=4339

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
/**
hdu 4339  线段树
题目大意:给定两个字符串进行两种操作,1 对第a个字符串的第b+1个字符换成c。2 询问从第i+1个字符开始两个字符串的最大匹配长度
解题思路:线段树维护区间最小不匹配点,单点更新,区间求取给定区间[i,len]的最小值。
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1000010;

char s[2][maxn];
int n,INF;

struct segtree
{
    int left,right,acount;
}tree[maxn*4];

void build(int r,int a,int b)
{
    tree[r].left=a,tree[r].right=b;
    tree[r].acount=INF;
    if(a<b)
    {
        int m=(a+b)>>1;
        build(r<<1,a,m);
        build(r<<1|1,m+1,b);
    }
}

void update(int r,int i,int key)
{
    if(tree[r].left==tree[r].right)
    {
        tree[r].acount=key;
        return;
    }
    int m=(tree[r].left+tree[r].right)>>1;
    if(i<=m)
    {
        update(r<<1,i,key);
    }
    else
    {
        update(r<<1|1,i,key);
    }
    tree[r].acount=min(tree[r<<1].acount,tree[r<<1|1].acount);
}

int find_min(int r,int a,int b)
{
    if(a<=tree[r].left&&tree[r].right<=b)
        return tree[r].acount;
    int m=(tree[r].left+tree[r].right)>>1;
    if(b<=m)
        return find_min(r<<1,a,b);
    else if(a>m)
        return find_min(r<<1|1,a,b);
    else
        return min(find_min(r<<1,a,m),find_min(r<<1|1,m+1,b));
}

int  main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s%s",s[0],s[1]);
        int len=min(strlen(s[0]),strlen(s[1]));
        INF=len+1;
        build(1,1,len);
        int i;
        for(i=0;i<len;i++)
        {
            if(s[0][i]!=s[1][i])
                update(1,i+1,i+1);
            else
                update(1,i+1,INF);
        }
        printf("Case %d:\n",++tt);
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            int p;
            scanf("%d",&p);
            if(p==2)
            {
                int j;
                scanf("%d",&j);
                if(j>=len)//!!!
                    printf("0\n");
                else
                    printf("%d\n",find_min(1,j+1,len)-j-1);

            }
            else if(p==1)
            {
                int q,j;
                char c[3];
                scanf("%d%d%s",&q,&j,c);
                if(j>=len)continue;
                q--;
                if(s[q][j]==s[q^1][j]&&s[q^1][j]!=*c) update(1,j+1,j+1);//更新前相同,更新后不同
                if(s[q][j]!=s[q^1][j]&&s[q^1][j]==*c) update(1,j+1,INF);//更新前不同,更新后相同
                s[q][j]=*c;
            }
        }
    }
    return 0;
}
/**
hdu 4339 mulitset
解题思路:不匹配的点存入multiset。
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
const int maxn=1000010;

char s[2][maxn];
int n;

int main()
{
    int T,tt=0;
    multiset<int>ms;
    multiset<int>::iterator it;
    scanf("%d",&T);
    while(T--)
    {
        ms.clear();
        scanf("%s%s",s[0],s[1]);
        int len1=strlen(s[0]);
        int len2=strlen(s[1]);
        int i;
        for(i=0; i<len1&&i<len2; i++)
        {
            if(s[0][i]!=s[1][i])
                ms.insert(i);
        }
        ms.insert(i);
        printf("Case %d:\n",++tt);
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            int p;
            scanf("%d",&p);
            if(p==2)
            {
                int j;
                scanf("%d",&j);
                it=ms.lower_bound(j);
                printf("%d\n",*it-j);
            }
            else
            {
                int q,j;
                char c[2];
                scanf("%d%d%s",&q,&j,c);
                q--;
                if(s[0][j]!=s[1][j])
                    ms.erase(j);
                s[q][j]=*c;
                if(s[0][j]!=s[1][j])
                    ms.insert(j);
            }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值