G. Query on a string (2017乌鲁木齐网络赛)(树状数组)

You have two strings SSS and TTT in all capitals.

Now an efficient program is required to maintain a operation and support a query.

The operation C i chC~i~chC i ch with given integer iii and capital letter chchch, changes the iii-th character of SSS into chchch.

The query Q i jQ~i~jQ i j asks the program to find out, in the substring of SSS from the iii-th character to the jjj-th one, the total number of TTT appearing.

Input Format

The first line contains an integer TTT, indicating that there are TTT test cases.

For each test case, the first line contains an integer N (N≤100000)N~(N \leq 100000)N (N≤100000).

The second line is the string S (∣S∣≤100000)S~(|S| \leq 100000)S (∣S∣≤100000) and the third line is the string T (∣T∣≤10)T~(|T| \leq 10)T (∣T∣≤10).

Each of the following NNN lines provide a operation or a query as above descriptions.

Output Format

For each query, output an integer correponding to the answer.

Output an empty line after each test case.

样例输入

1
5
AABBABA
AA
Q 1 3
C 6 A
Q 2 7
C 2 B
Q 1 5

样例输出

1
2
0

题目来源

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛

题目大意:输入T组样例,输入一个n表示有n此询问,给出一个文本串和一个模式串。在n此询问中,如果是Q,则输入了l, r, 询问文本串在l到r区间内有多少个字串为模式串。如果是C,输入一个整数l, 一个字符ch, 把文本串中的l位置的字符改为ch。

解题思路:用树状数组维护区间内存在模式串的个数。每次更改文本串后,判断是否对树状数组维护的数组产生影响,如果有影响则更新。需要注意的是,这个题好像是不能用C++的输入输出流,否则超时!

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
const int Maxn = 1e5 + 10 ;

string ss[Maxn] ;
int n, bit[Maxn], len, m ;

inline int lowbit(int i){ return i & (-i) ;}

inline void add (int i, int x){
    while (i <= m){
        bit[i] += x;
        i += lowbit(i) ;
    }
}

inline int sum (int x){
    int ans = 0 ;
    while (x){
        ans += bit[x] ;
        x -= lowbit(x) ;
    }
    return ans ;
}

int main (){
//    ios_base::sync_with_stdio(false) ;
//    cin.tie(0) ;
//    cout.tie(0) ;
    int T ;
    scanf("%d", &T) ;
    while (T--){
        char s1[Maxn], s2[Maxn] ;
        scanf("%d", &n) ;
        scanf("%s", s1) ;
        string str(s1) ;
        scanf("%s", s2) ;
        string s(s2) ;
        len = s.size() ;
        memset(bit, 0, sizeof(bit)) ;
        m = str.size() - len + 1 ;
        for (int i = 0; i < m; i++){
            for (int j = 0; j < len; j++) ss[i + 1] += str[i + j] ;
    //        cout << ss << endl ;
            if (ss[i + 1] == s) add(i + 1, 1) ;
        }
        while (n--){
            char op[2] ;
//            cin >> op ;
            scanf("%s", op) ;
            if (op[0] == 'C'){
                int l ;
                char ch[2] ;
                scanf("%d%s", &l, ch) ;
                for (int i = max(l - len + 1, 1); i <= l; i++){
                    string pre = ss[i] ;
//                    cout << "++++" << ss[i] << endl ;
                    ss[i][l - i] = ch[0] ;
//                    cout << "----" << ss[i] << endl ;
                    if (pre == s && ss[i] != s) add(i, -1) ;
                    else if (pre != s && ss[i] == s) add(i, 1) ;
                }
            }
            else {
                int l, r ;
                scanf("%d%d", &l, &r) ;
                if (l > r) swap(l, r) ;
                int x = r - len + 1 ;
                int ans ;
                if (x < l) ans = 0 ;
                else ans = sum(x) - sum(l - 1) ;
                printf("%d\n", ans) ;
            }
        }
        for (int i = 0; i < m; i++) ss[i + 1].clear() ;
        puts("") ;
    }
    return 0 ;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值