CodeForces - 963D:Frequency of String (bitset暴力搞)
You are given a string ss. You should answer nn queries. The ii-th query consists of integer kiki and string mimi. The answer for this query is the minimum length of such a string tt that tt is a substring of ss and mimi has at least kiki occurrences as a substring in tt.
A substring of a string is a continuous segment of characters of the string.
It is guaranteed that for any two queries the strings mimi from these queries are different.
Input
The first line contains string ss (1≤|s|≤105)(1≤|s|≤105).
The second line contains an integer nn (1≤n≤1051≤n≤105).
Each of next nn lines contains an integer kiki (1≤ki≤|s|)(1≤ki≤|s|) and a non-empty string mimi — parameters of the query with number ii, in this order.
All strings in input consists of lowercase English letters. Sum of length of all strings in input doesn't exceed 105105. All mimi are distinct.
Output
For each query output the answer for it in a separate line.
If a string mimi occurs in ss less that kiki times, output -1.
Examples
aaaaa
5
3 a
3 aa
2 aaa
3 aaaa
1 aaaaa
3
4
4
-1
5
abbb
7
4 b
1 ab
3 bb
1 abb
2 bbb
1 a
2 abbb
-1
2
-1
3
-1
1
-1
题意:给定串S,N次询问,每次求最短的子串,使得s出现次数等于k。
思路:后缀自动机,AC自动机,hash都可以做;bitset,然后尺取法。有点暴力,不过CF跑得过去。
思路2:考虑到串的长度种类不超过NsqrtN种,我们可以把这些sqrtN种长度的hash都保存起来,以hash值维第一关键字,以坐标为第二关键字,排序。然后对于每个询问,我们lower_bound到这一类hash值的位置,然后尺取法得到最小答案。
关键还是要回函数bitset._Find_first(),和bitset._Find_next(i)
#include<bits/stdc++.h>
#define N 100005
using namespace std;
char s[N],t[N];
bitset<N>b[],tmp;
int main(){
scanf("%s",s);
int n=strlen(s);
for(int i=;i<n;i++)
b[s[i]-'a'][i]=;
int Q; scanf("%d",&Q);
while (Q--){
int k; scanf("%d%s",&k,t);
int m=strlen(t);
tmp.set();
for(int i=;i<m;i++) tmp&=b[t[i]-'a']>>i;
if (tmp.count()<k){ puts("-1"); continue;}
vector<int> v;
for(int i=tmp._Find_first();i<n;i=tmp._Find_next(i))
v.push_back(i);
int ans=1e9;
for (int i=;i+k-<v.size();i++)
ans=min(ans,v[i+k-]-v[i]+m);
printf("%d\n",ans);
}
return ;
}
CodeForces - 963D:Frequency of String (bitset暴力搞)的更多相关文章
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- cf914F. Substrings in a String(bitset 字符串匹配)
题意 题目链接 Sol Orz jry 和上一个题一个思路吧,直接bitset乱搞,不同的是这次有了修改操作 因为每次修改只会改两个位置,直接暴力改就好了 #include<bits/stdc+ ...
- Frequency of String CodeForces - 963D
http://codeforces.com/contest/963/problem/D 题解:https://www.cnblogs.com/Blue233333/p/8881614.html 记M为 ...
- 【CodeForces】914 F. Substrings in a String bitset
[题目]F. Substrings in a String [题意]给定小写字母字符串s,支持两种操作:1.修改某个位置的字符,2.给定字符串y,查询区间[l,r]内出现y多少次.|s|,Σ|y|&l ...
- Codeforces 34C-Page Numbers(set+vector+暴力乱搞)
C. Page Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)D. Frequency of String
题意:有一个串s,n个串模式串t,问s的子串中长度最小的包含t k次的长度是多少 题解:把所有t建ac自动机,把s在ac自动机上匹配.保存每个模式串在s中出现的位置.这里由于t两两不同最多只有xsqr ...
- Codeforces Gym 100002 C "Cricket Field" 暴力
"Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1000 ...
- Codeforces 868D Huge Strings - 位运算 - 暴力
You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...
- HDU 5506:GT and set bitset+暴力
GT and set Accepts: 35 Submissions: 194 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 655 ...
随机推荐
- mapreduce 运行-指定各种运行参数
mapreduce指定参数 mapreduce在运行的时候可以指定各种参数,这样可以根据实际的应用场景做一下相关的调整 1.指定运行时cpu的个数 hadoop jar hadoop-core-0.1 ...
- Sybase:delete与truncate、drop区别
Sybase:delete与truncate.drop区别 区别: TRUNCATE TABLE TABLENAME:删除内容.释放空间但不删除定义. DELETE FROM TABLENAME:删除 ...
- centos 6+安装山逗斯骚尅特
系统支持:CentOS 6+,Debian 7+,Ubuntu 12+ 内存要求:≥128M 关于本脚本 一键安装 Shadowsocks-Python, ShadowsocksR, Shadowso ...
- Python学习之:pycharm配置
最近需要做一些小工具,听说Python不错,就学习一下.工欲善其事必先利其器,一个好的IDE对于学习一门新知识是很有帮助的,边写代码边换IDE,纠结了几天,最终还是选择了pycharm,之前觉得不够好 ...
- Linux系统基本的内存管理知识讲解
内存是Linux内核所管理的最重要的资源之一.内存管理系统是操作系统中最为重要的部分,因为系统的物理内存总是少于系统所需要的内存数量.虚拟内存就是为了克服这个矛盾而采用的策略.系统的虚拟内存通过在各个 ...
- vi使用技巧(转载)
http://www.cnblogs.com/xusir/p/3245007.html 这是转载的链接
- NUnit单元测试笔记
vs2010 和 NUnit 问题处理. . 在 <configuration> 下 加 ... <startup> <requiredRuntime version=& ...
- 使用 <!-- 指定使用hibernate核心配置文件 --> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
在bean.xml文件中,这样使用出现问题 <!-- 指定使用hibernate核心配置文件 --> <property name="configLocations&quo ...
- 绘制三角形(sass)
绘制三角形 /// draw triangle/// @param {type} $type [''] - triangleUp triangleDown triangleLeft triangleR ...
- Ubuntu16.04 Kdevelop汉化及配置
关闭Kdevelop sudo apt-get install kdevelop-l10n 再打开. 字体选择 Sans Serif :style:Normal:这样更舒服且不影响中文的排版,如何改成 ...