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(" ...
随机推荐
- 什么是 Linux
什么是Linux Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络 ...
- 使用icomoon把svg图片生成字体图标
今天看了使用icomoon来将svg转换成图标字体,本来是不会使用别人给的svg,也不清楚具体的好处是什么,查了svg以后,越来越懵,svg挺好的为什么要转成图标字体呢. 一.SVG介绍 SVG 是一 ...
- Linux最常用的命名
一.环境配置 vim /etc/sysconfig/network-scripts/ifcfg-eth0 vim /etc/sysconfig/network vim /etc/hostname vi ...
- 1054. 求平均值 (20)-PAT乙级真题
今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org ...
- Android 系统广播机制
一.Android应用程序注冊广播接收器(registerReceiver)的过程分析 參考Android应用程序注冊广播接收器(registerReceiver)的过程分析http://blog.c ...
- invlpg 指令简单介绍
invlpg 指令简单介绍 void tlb_invalidate(pde_t *pgdir, void *va) { // Flush the entry only if we're modifyi ...
- 手写 redux 和 react-redux
1.手写 redux redux.js /** * 手写 redux */ export function createStore(reducer) { // 当前状态 let currentStat ...
- UML类间关系
1继承 指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父接口)的功能,并可以增加它自己的新功能的能力,继承是类与类或者接口与接口之间最常见的关系:在Java中此类关系通过关键字exten ...
- 运营或生态链没做好,APP质量再高有个鸟用(下)
上篇文章已经阐述了对于一款产品卖不卖作,事实上非常依赖于运营的打造和生态链的建立,这里能够解释为什么很多优秀的游戏产品功底非常好,但開始并不卖作,仅仅有碰到一家肯出力推的渠道游戏才迅速火了起来.这是不 ...
- 不能选择sublime作为默认打开方式的解决办法
Sublime Text 绿色版删除后无法设置为默认打开方式…而且网上也没有给出明确的解决办法 注册表的解决办法: 删除 HKEY_CURRENT_USER\Software\Classes\Appl ...