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 ...
随机推荐
- 微信小程序获取验证码倒计时
getVerificationCode: function() { var that = this; var currentTime = that.data.currentTime; that.set ...
- JSONP跨站访问
js中几种实用的跨域方法原理详解 这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协 ...
- Hadoop:eclipse配置hadoop-eclipse-plugin(版本hadoop2.7.3)
配置hadoop-eclipse-plugin(版本hadoop2.7.3): 1:首先下载我们需要的 hadoop-eclipse-plugin-2.7.3.jar,winutils.exe 和 ...
- Mac & Xcode 技巧
1.Xcode 删除过期的Provisioning Profile文件 解决:Xcode 中所有的Provisioning Profile文件路径,都在 ~/Library/MobileDevice/ ...
- File类之常用方法
package IoDemo; import java.io.File; import java.io.IOException; /** * @Title:FileTest * @Descriptio ...
- geoserver源码学习与扩展——CSV转shapefile文件
基于geotools实现csv转换为shapefile文件. 1.读取CSV文件,将其装入FeatureCollection: 2.利用ShapefileDumper类将FeatureCollecti ...
- spring的AOP动态代理--JDK代理和CGLIB代理区分以及注意事项
大家都知道AOP使用了代理模式,本文主要介绍两个代理模式怎么设置以及区别,对原文一些内容进行了引用后加入了自己的理解和更深入的阐述: 一.JDK代理和CGLIB代理的底层实现区别* JDK代理只能 ...
- LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
- MySQL配置管理与安装方法
数据库的安装: 版本:SQL2008 R2(下载地址为:http://www.accessoft.com/article-show.asp?id=11192) 这里说明一下: NT Authority ...
- 使用Entity Framework时遇到的各种问题总结
在这里记录一下之前使用Entity Framework(4.3.1版本)遇到的问题. 更新没有设置主键的表 在默认情况下,EF不能对一个没有主键的表进行更新.插入和删除的动作.用xml方式查看edmx ...