Codeforces-914F Substrings in a String (Bitset求T串中S串出现次数)
之前有过区域赛,简化版问题:
给定一个小写字符组成的字符串S,(|S|<1e5,下标从1开始),现在有Q种操作,对于每个操作Q(Q<=1e5),输入opt,
如果opt==1,输入x,c,表示把S[x]改为c,(c是小写字母)。
如果opt==2,输入L,R,和字符串T,输出S[L-R]中有多少个字串==T(字符串可以重叠),(|T|<=100)。
(其中opt==2的询问次数小于1e3)
-------------------------------------------分界线-------------------------------------
此题:
Given a string s, process q queries, each having one of the following forms:
- 1 i c — Change the i-th character in the string to c.
- 2 l r y — Consider the substring of s starting at position l and ending at position r. Output the number of times y occurs as a substring in it.
Input
The first line of the input contains the string s (1 ≤ |s| ≤ 105) of lowercase English letters.
The second line contains an integer q (1 ≤ q ≤ 105) — the number of queries to process.
The next q lines describe the queries and may have one of the following forms:
- 1 i c (1 ≤ i ≤ |s|)
- 2 l r y (1 ≤ l ≤ r ≤ |s|)
c is a lowercase English letter and y is a non-empty string consisting of only lowercase English letters.
The sum of |y| over all queries of second type is at most 105.
It is guaranteed that there is at least one query of second type.
All strings are 1-indexed.
|s| is the length of the string s.
Output
For each query of type 2, output the required answer in a separate line.
Example
ababababa
3
2 1 7 aba
1 5 c
2 1 7 aba
3
1
abcdcbc
5
2 1 7 bc
1 4 b
2 4 7 bc
1 2 a
2 1 4 aa
2
2
1
Note
Consider the first sample case. Initially, the string aba occurs 3 times in the range [1, 7]. Note that two occurrences may overlap.
After the update, the string becomes ababcbaba and now aba occurs only once in the range [1, 7].
思路:题意和上面的差不多,不过数据更大一点,只有Bitset或者分块优化(后者我没有试过)。
具体的:1,假设我们要统计S里有多少个T,先统计S里面字符==T[0]的是哪些,然后统计S中有T[0]的位置后面跟的字符==T[1]的有哪些,然后统计S中有T[0]的位置后面跟的字符==T[1]的而且后面跟的字符==T[2]的有哪些.....直到对比到S[len-1]。
2,最后利用位移可以得到某个区间的1的个数。
for(i=;i<S;i++)
ans&=(bitset[T[i]-'a']>>i);
ans开始全部是1; bitset是保存的T串里每个字符的位置;
(目测还有非常高效的方法,提交列表里有效率为5倍以上的,还有待学习)
#include<bitset>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
bitset<maxn>s[],ans;
char a[maxn],b[maxn],c[];
void read(int &res)
{
char c=getchar();
while(c>''||c<'') c=getchar();
for(res=;c>=''&&c<='';c=getchar()) res=(res<<)+(res<<)+c-'';
}
int main()
{
scanf("%s",a+);
int T=strlen(a+);
int i,j,l,r,Q,opt;
for(i=;i<=T;i++)
s[a[i]-'a'].set(i);
read(Q);
while(Q--){
read(opt);
if(opt==){
scanf("%d%s",&j,c);
s[a[j]-'a'][j]=;
s[(a[j]=c[])-'a'][j]=;
}
else {
read(l); read(r);
scanf("%s",b);
int S=strlen(b);
if(S>r-l+) {
puts(""); continue;
}
ans.set();
for(i=;i<S;i++){
ans&=(s[b[i]-'a']>>i);
}
printf("%d\n",(ans>>l).count()-(ans>>(r-S+)).count());
}
}
return ;
}
Codeforces-914F Substrings in a String (Bitset求T串中S串出现次数)的更多相关文章
- Codeforces 914F. Substrings in a String(bitset)
比赛的时候怎么没看这题啊...血亏T T 对每种字符建一个bitset,修改直接改就好了,查询一个区间的时候对查询字符串的每种字符错位and一下,然后用biset的count就可以得到答案了... # ...
- CF 914F Substrings in a String——bitset处理匹配
题目:http://codeforces.com/contest/914/problem/F 可以对原字符串的每种字母开一个 bitset .第 i 位的 1 表示这种字母在第 i 位出现了. 考虑能 ...
- HihoCoder1084: 扩展KMP(二分+hash,求T串中S串的数量,可以失配一定次数)
时间限制:4000ms 单点时限:4000ms 内存限制:256MB 描述 你知道KMP吗?它是用于判断一个字符串是否是另一个字符串的子串的算法.今天我们想去扩展它. 在信息理论中,在两个相同长度的字 ...
- 【CodeForces】914 F. Substrings in a String bitset
[题目]F. Substrings in a String [题意]给定小写字母字符串s,支持两种操作:1.修改某个位置的字符,2.给定字符串y,查询区间[l,r]内出现y多少次.|s|,Σ|y|&l ...
- Codeforces 917F Substrings in a String - 后缀自动机 - 分块 - bitset - KMP
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个字母串,要求支持以下操作: 修改一个位置的字母 查询一段区间中,字符串$s$作为子串出现的次数 Solution 1 Bitset 每 ...
- Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量
Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...
- cf914F. Substrings in a String(bitset 字符串匹配)
题意 题目链接 Sol Orz jry 和上一个题一个思路吧,直接bitset乱搞,不同的是这次有了修改操作 因为每次修改只会改两个位置,直接暴力改就好了 #include<bits/stdc+ ...
- zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)
/** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...
- 任意输入一串字符串,求该字符串中字符的出现次数并打印出来,如输入“bcaba”输出:b 2 c 1 a 2
前言:其实我还是有点不懂,有点郁闷了,算了直接把代码放上去把. 方法一: Scanner input=new Scanner(System.in); System.out.println(" ...
随机推荐
- 使用Sharesdk实现第三方平台登录(qq,新浪微博)
首先到sharesdk开放píng台下载demo ,以下要用到的文件来自于 simple里面 第一步:导入官方的jar包 第二步:添加ShareSDK.xml文件并修改相关píng台key 第 ...
- Docker-PostgresSQL
Postgresql Docker安装运行 mac环境: 1.拉取官方镜像,并创建容器 zhoumatoMacBook-Pro:~ zhou$ docker search postgresql NA ...
- SGU103+POJ 1158 最短路/dp
题意:一个无向图,求起点到终点最少时间,限制:每个路口有灯,要灯颜色一样才能过去,灯之有俩种颜色,周期 变化,给定每个灯初态,时间. 思路:开始就想到直接DP,方程dp[k]=dp[i]+distan ...
- 三种Model模式
目前项目中可能出现的三种Model模式,对于我们现在开发的一个项目,我觉得使用DDD的思想来设计模型比较清晰,使用DDD的思想把模型model分成了如下三种:ViewModel,它与页面相关,Doma ...
- python文件追加及时间获取
一.python:文件的读取.创建.追加.删除.清空 2011-10-24 11:36:35| 分类: python |举报 |字号 订阅 一.用Python创建一个新文件,内容是从0到9的整数 ...
- Codeforces Round #310 (Div. 2)简洁题解
A:原来是大水题,我还想去优化.. 结果是abs(num('0')-num('1')); num表示一个符号的个数; B:暴力模拟即可,每次判断是否能构造出答案. C:俄罗斯套娃,套套套,捉鸡的E文. ...
- the import org.springframewok.test cannot be resolved
在写Spring的单元测试时遇见了问题,注解@ContextConfiguration和SpringJUnit4ClassRunner.class无法导包.手动导包后错误为“the import or ...
- KernelHacking
https://kernelnewbies.org/KernelHacking-HOWTO/Debugging_Kernel
- chromium爱好者不可错过的一个开源分支
这次我要推荐下http://bloomberg.github.com/chromium.bb, 名字就叫chromium.bb,特点是专门的windows ports,关键是极大的简化了原版chrom ...
- XStream 数组(List)输出结构
<!-- 期望的DOM树 --> <Articles> <item> <Title>微信SDK初步结构</Title> <Descri ...