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\n", 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;
}

时间: 2024-10-26 19:27:01

18000 Two String 暴力。——— 读题的相关文章

hdu 2079 选课时间(题目已修改,注意读题) 多重背包

选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3162    Accepted Submission(s): 2472 Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别

poj1789 MST 读题生涯永不停歇

题目: 链接在此 1.图论刷刷乐#1的第一题,无奈看了好长时间题目还是看不懂= =,明知是最水的题目 2.搜懂题目后,比较裸的MST,但还是决定写个题解,虽然没什么可说的,只是来警戒自己,还是要努力读题,YY大法,这也是水平的一个体现! #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include &

ACM 暴力搜索题 题目整理

UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vector> #include<cmath> #include<map> #include<algorithm> #include<cstring> #include<cstdio> #include<cstdlib> #include

HDU 2079 选课时间(题目已修改,注意读题)【母函数】

选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3250    Accepted Submission(s): 2551 Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别

选课时间(题目已修改,注意读题)(杭电2079)(母函数)

选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3024    Accepted Submission(s): 2365 Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别

HDU 4572 Bottles Arrangement(找规律,仔细读题)

题目 //找规律,123321123321123321…发现这样排列恰好可以错开 // 其中注意题中数据范围: M是行,N是列,3 <= N < 2×M //则猜测:m,m,m-1,m-1,m-2,m-2,……,2,2,1,1求出前m个数字的和就是答案. //发现案例符合(之前的代码第二天发现案例都跑不对,真不知道我当时眼睛怎么了) #include <iostream> #include<stdio.h> #include<string.h> #inclu

HDU 4930 Fighting the Landlords (超级暴力+读懂题意)

题目链接:HDU 4930 Fighting the Landlords 斗地主!!.不会玩这游戏,真是苦逼.题意其他都还好,就是要注意只要第一个回合1号玩家能压制2号玩家就算赢了(突破点). 其他就分类暴力了,思路还是比较清晰的. 注意点: 1.对方炸弹,必输 2.一回合就出完牌,必胜 AC代码: #include<stdio.h> #include<string.h> int vis1[30],vis2[30]; int find(char s) { if(s=='T') re

hdu 5641 King&#39;s Phone(暴力模拟题)

Problem Description In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as

hdu2098分拆素数和(读题很重要!!!)

分拆素数和 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2098 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 39300    Accepted Submission(s): 17193 Problem Description 把一个偶数拆成两个不同素数的和,有几种拆法呢? Input