zoukankan      html  css  js  c++  java
  • 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
    ", 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;
        }
    }
  • 相关阅读:
    【python刷题】前缀和
    【python刷题】数组链表去重
    【python刷题】滑动窗口法
    【python刷题】二分查找
    【python刷题】广度优先搜索(BFS)
    【python刷题】回溯算法(深度优先搜索DFS)
    机器学习十讲-第三讲分类
    数学知识-质数&约数
    树与图的DFS与BFS
    桥梁保护与监控-开发进度(二)
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7515241.html
Copyright © 2011-2022 走看看