Query on a string

 

You have two strings SS and TT in all capitals.

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

The operation C~i~chC i ch with given integer ii and capital letter chch, changes the ii-th character of SSinto chch.

The query Q~i~jQ i j asks the program to find out, in the substring of SS from the ii-th character to the jj-th one, the total number of TT appearing.

Input Format

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

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

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

Each of the following NN 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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<functional>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

const int MAXN = 100000 + 33;
char s[MAXN];//字符串
bool been[MAXN];//结尾记录是否匹配 匹配为1,不匹配为0
char t[100];
int T, n, a[MAXN];
inline int lowbit(int x)
{
    return x&(-x);
}
int getsum(int x)
{
    if (x == 0) return 0;
    int sum = 0;
    while (x > 0)
    {
        sum += a[x];
        x -= x&(-x);
    }
    return sum;
}
void update(int x, int val)
{
    while (x < MAXN)
    {
        a[x] += val;
        x += x&(-x);
    }
}
int main()
{
    //ios::sync_with_stdio(0);
    scanf("%d", &T);
    int u, v;
    char op[2], to[2];
    while (T--)
    {
        memset(been, false, sizeof(been));
        memset(a, 0, sizeof(a));
        scanf("%d%s%s", &n, s + 1, t + 1);
        int l = strlen(t + 1), L = strlen(s + 1);
        for (int i = 1; i + l - 1 <= L; i++)
        {
            int cnt = 1;
            while (cnt <= l&&s[i + cnt - 1] == t[cnt])cnt++;
            if (cnt > l)
            {
                update(i + l - 1, 1);
                been[i + l - 1] = true;
            }
        }
        //for (int i = 1; i <= L; i++)
        //    cout << getsum(i) << endl;
        while (n--)
        {
            scanf("%s", op);
            if (op[0] == 'Q')
            {
                scanf("%d%d", &u, &v);
                printf("%d\n", max(0, getsum(v) - getsum(u + l - 1- 1)));
            }
            else if(op[0] == 'C')
            {
                scanf("%d%s", &u, to);
                s[u] = to[0];
                int len = max(1, u - l + 1);
                for (int i = len; i <= u; i++)
                {
                    if (i + l - 1> L) break;
                    int cnt = 1;
                    while (cnt <= l&&s[i + cnt - 1] == t[cnt])cnt++;
                    if (cnt > l)
                    {
                        if (been[i + l - 1]) continue;
                        been[i + l - 1] = 1;
                        update(i + l - 1, 1);
                    }
                    else if (been[i + l - 1])
                    {
                        been[i + l - 1] = 0;
                        update(i + l - 1, -1);
                    }
                }
            }
        }
        cout << endl;
    }
}

 

转载于:https://www.cnblogs.com/joeylee97/p/7515241.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值