• 18000 Two String 暴力。——— 读题


    http://acm.scau.edu.cn:8000/uoj/mainMenu.html

    18000 Two String

    时间限制:1000MS  内存限制:65535K
    提交次数:0 通过次数:0

    题型: 编程题   语言: 不限定

     

    Description

    Given two string A and B and three kinds of operations as following:
    (1)Push_back c            add a character c at the back of string B
    (2)Push_front c           add a character c in front of string B
    (3)Query                  calculate and output how many times B appears in A
    could you calculate and output the right answer for each query? 
    




    输入格式

    The first line contains the string A ,the second line contains the string B ,the third line contains an integer M (1 <= M <= 2000),
    expressing the number of operation, Each of the following M lines contains one of the above-mentioned three operations. total length of the string does not exceed 5000,all character in the input are the lower case Latin alphabet



    输出格式

    For each query, output a line containing the answer.



     

    输入样例

    abcabc
    a
    5
    Query
    Push_back b
    Query
    Push_front a
    Query
    aaaaa
    a
    5
    Query
    Push_back a
    Query
    Push_front a
    Query



     

    输出样例

    2
    2
    0
    5
    4
    3



     

    来源

     星尘 

     

    作者

     admin

    一定要注意到的是,

    total length of the string does not exceed 5000,

    就是所有样例的字符全加起来不会超过5000,其实我觉得这样给数据范围很坑爹。不如一个样例一个样例给我。

    一直不敢做,其实就是暴力。

    对于每种push,暴力进行。

    每种查询,kmp一次。

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    const int maxn = 10000 + 20;
    char str[maxn];
    char sub[2][maxn];
    int lensub;
    int lenstr;
    int now;
    int tonext[maxn];
    void get_next(int now) {
        int i = 1, j = 0;
        tonext[1] = 0;
        while (i <= lensub) {
            if (j == 0 || sub[now][i] == sub[now][j]) {
                tonext[++i] = ++j;
            } else j = tonext[j];
        }
    }
    int kmp(int now) {
        get_next(now);
        int i = 1, j = 1;
        int ans = 0;
        while (i <= lenstr) {
            if (j == 0 || str[i] == sub[now][j]) {
                ++i;
                ++j;
            } else j = tonext[j];
            if (j == lensub + 1) {
                ans++;
                j = tonext[j];
            }
        }
        return ans;
    }
    void work() {
        lenstr = strlen(str + 1);
        lensub = strlen(sub[now] + 1);
        int q;
        scanf("%d", &q);
        char t[22];
        for (int i = 1; i <= q; ++i) {
            scanf("%s", t + 1);
            if (t[1] == 'Q') {
                printf("%d
    ", kmp(now));
            } else {
                char tt[23];
                scanf("%s", tt);
                if (strcmp("Push_back", t + 1) == 0) {
                    sub[now][++lensub] = tt[0];
                } else {
                    for (int i = 1; i <= lensub; ++i) {
                        sub[!now][i + 1] = sub[now][i];
                    }
                    sub[!now][1] = tt[0];
                    now = !now;
                    lensub += 1;
                }
            }
        }
    }
    int main() {
    #ifdef local
        freopen("data.txt","r",stdin);
    #endif
        while (scanf("%s%s", str + 1, sub[now] + 1) != EOF) work();
        return 0;
    }
    View Code
  • 相关阅读:
    控制器生命周期逻辑调用
    数据持久化
    Mac屏幕录制Gif
    iOS开发应用上架必读最新苹果审核规则
    过滤字符串中的非汉字、字母、数字
    文字加描边
    博客全局修改需求
    iOS Xcode12 运行iOS15系统程序卡在启动页要等很久才能进入主页
    macOS环境:安装Go(21-10-22完)
    关闭WIN10自动配置 IPV4 地址 169.254解决方法
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6052913.html
Copyright © 2020-2023  润新知