首页 > 代码库 > HDU 4339 Query

HDU 4339 Query

Query

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2595    Accepted Submission(s): 860


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
 

Source
2012 Multi-University Training Contest 4

解题思路:用<set>保存字符不同的位置,修改的话直接改,查的话用lower_bound,注意要把字符串长度len也添加进去,这是为了避免字符串全部相等的情况
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#define Maxn 1000005
using namespace std;
char str1[Maxn],str2[Maxn];
int main()
{
    int t,ncase=1,q,a,c,b;
    char ch;
    set<int> s;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%d",&t);
    while(t--)
    {
        s.clear();
        set<int>::iterator it;
        scanf("%s%s",str1,str2);
        scanf("%d",&q);
        int len=strlen(str1);
        for(int i=0;i<len;i++)
            if(str1[i]!=str2[i])
                s.insert(i);
        s.insert(len);
        printf("Case %d:\n",ncase++);
        while(q--)
        {
            scanf("%d",&a);
            if(a==1)
            {
                scanf("%d %d %c\n",&b,&c,&ch);
                if(b==1)
                    str1[c]=ch;
                else
                    str2[c]=ch;
                if(str1[c]!=str2[c])
                    s.insert(c);
                else
                {
                    if(s.find(c)!=s.end())
                        s.erase(c);
                }
            }
            else
            {
                scanf("%d",&b);
                it=s.lower_bound(b);
                printf("%d\n",*it-b);
            }
        }
    }
    return 0;
}


 

HDU 4339 Query