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 = + ;
char str[maxn];
char sub[][maxn];
int lensub;
int lenstr;
int now;
int tonext[maxn];
void get_next(int now) {
int i = , j = ;
tonext[] = ;
while (i <= lensub) {
if (j == || sub[now][i] == sub[now][j]) {
tonext[++i] = ++j;
} else j = tonext[j];
}
}
int kmp(int now) {
get_next(now);
int i = , j = ;
int ans = ;
while (i <= lenstr) {
if (j == || str[i] == sub[now][j]) {
++i;
++j;
} else j = tonext[j];
if (j == lensub + ) {
ans++;
j = tonext[j];
}
}
return ans;
}
void work() {
lenstr = strlen(str + );
lensub = strlen(sub[now] + );
int q;
scanf("%d", &q);
char t[];
for (int i = ; i <= q; ++i) {
scanf("%s", t + );
if (t[] == 'Q') {
printf("%d\n", kmp(now));
} else {
char tt[];
scanf("%s", tt);
if (strcmp("Push_back", t + ) == ) {
sub[now][++lensub] = tt[];
} else {
for (int i = ; i <= lensub; ++i) {
sub[!now][i + ] = sub[now][i];
}
sub[!now][] = tt[];
now = !now;
lensub += ;
}
}
}
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
while (scanf("%s%s", str + , sub[now] + ) != EOF) work();
return ;
}

最新文章

  1. linux memcached安装
  2. android相机调用及存储详解
  3. ethereal抓包工具
  4. Java方法-数组
  5. WPF控件---Border应用
  6. OGG常见问题处理
  7. mysql 字段注释
  8. spring @Component
  9. PHP中json_encode与json_decode
  10. Java finalize方法使用
  11. .net core Razor视图的TagHelper使用方法介绍
  12. oracle 两张关联表执行更新update
  13. 搭建一个webpack微服务器
  14. HystrixCommand实战
  15. two sum --无脑法
  16. webpack 练习笔记
  17. XP下1433端口打不开
  18. .NetCore下使用Prometheus实现系统监控和警报 (一)介绍【译】
  19. SeekBar的用法和自定义滑块的样式
  20. Qt 立体水晶按键实现

热门文章

  1. maven中常用命令
  2. HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)
  3. js跳转方式 【转】
  4. BZOJ_1044_[HAOI2008]木棍分割_二分答案+DP+单调队列
  5. [AH2017/HNOI2017]抛硬币
  6. bzoj 5092 分割序列 —— 高维前缀和
  7. poj1275收银员——差分约束
  8. HDU3466(01背包变种)
  9. json : json数据解析(一)
  10. Paint Tree