【2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 G】Query on a string
2024-09-02 00:35:49
【链接】h在这里写链接
【题意】
让你维护字符串的一段区间内T子串的个数。
【题解】
因为t不大,所以。
暴力维护一下a[i]就好。
a[i]表示的是S串从i位置开始,能和T串匹配几个字符。
用树状数组维护区间内a[i]==lent的个数就好。
修改,还是暴力改就行。只会影响到那几个位置的。
【错的次数】
0
【反思】
匹配的部分写在一个函数里面比较好,用起来比较方便。
【代码】
#include <bits/stdc++.h>
using namespace std; const int N = 1e5;
struct BI {
int a[N + 10]; int lowbit(int x) {
return x&(-x);
} void add(int x, int y) {
while (x <= N) {
a[x] += y;
x += lowbit(x);
}
} int sum(int x) {
int now = 0;
while (x > 0) {
now += a[x];
x -= lowbit(x);
}
return now;
} int get_sum(int l, int r) {
return sum(r) - sum(l - 1);
} }BIT; char s[N + 20], t[20 + 20];
int Q,a[N+20],lens,lent; void pipei(int pos) {
a[pos] = 0;
for (int j = 1; j <= lent && pos + j - 1 <= lens; j++)
if (s[pos + j - 1] == t[j])
a[pos] = j;
else
break;
} void geta() {
for (int i = 1; i <= lens; i++) {
pipei(i);
if (a[i] == lent) BIT.add(i, 1);
}
} void in() {
scanf("%d", &Q);
scanf("%s%s", s + 1, t + 1);
lens = strlen(s + 1), lent = strlen(t + 1);
} void cl() {
for (int i = 1; i <= Q; i++) {
char r[5];
scanf("%s", r);
if (r[0] == 'Q') {
int a, b;
scanf("%d%d", &a, &b);
if (b - lent + 1 < a)
puts("0");
else
printf("%d\n", BIT.get_sum(a,b-lent+1));
}
else {
int pos;
scanf("%d%s", &pos, r);
s[pos] = r[0];
for (int j = max(1,pos - lent + 1); j <= pos; j++) {
int temp = a[j];
pipei(j);
if (temp == lent) {
if (a[j] != lent) {
BIT.add(j, -1);
}
}
else
if (a[j] == lent)
BIT.add(j, 1);
}
}
puts("");
}
} int main() {
//freopen("F:\\rush.txt", "r", stdin);
//ios::sync_with_stdio(0), cin.tie(0);
int T;
scanf("%d", &T);
while (T--) {
in();
geta();
cl();
}
return 0;
}
最新文章
- WCF服务器证书配置说明-没有能够进行密钥交换的私钥,或者进程可能没有访问私钥的权限
- CSS 自定义字体
- Java使用ZXing生成二维码条形码
- viewpager接受值图片轮播
- Java线程池的原理及几类线程池的介绍
- 树链剖分(单点更新,求区间最值,区间求和Bzoj1036)
- 静态wenb开发,动态web开发
- 自己动手写一个简单的(IIS)小型服务器
- Lattice Diamond安装
- classic asp中使用ADODB.Command防止sql injection
- Git简介及安装和简单配置
- schedule()函数的调用时机(周期性调度)
- qscoj 128 喵哈哈村的魔法源泉(2)(模仿快速幂,好题)
- redis 安装实战(10步完成安装)
- Python+Selenium基础篇-打开和关闭火狐浏览器
- 火车头采集器如何采集QQ群成员中的QQ号
- 自学Linux Shell4.1-监测程序ps top kill
- C++学习(七)(C语言部分)之 输入
- 浅谈 REST 和 RESTFul API
- 剑指offer:栈的压入、弹出序列