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

Input

aaaaa53 a3 aa2 aaa3 aaaa1 aaaaa

Output

344-15

Input

abbb74 b1 ab3 bb1 abb2 bbb1 a2 abbb

Output

-12-13-11-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[26],tmp;
int main(){
    scanf("%s",s);
    int n=strlen(s);
    for(int i=0;i<n;i++)
        b[s[i]-‘a‘][i]=1;
    int Q; scanf("%d",&Q);
    while (Q--){
        int k; scanf("%d%s",&k,t);
        int m=strlen(t);
        tmp.set();
        for(int i=0;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=0;i+k-1<v.size();i++)
            ans=min(ans,v[i+k-1]-v[i]+m);
        printf("%d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/hua-dong/p/9693542.html

时间: 2024-08-02 01:04:35

CodeForces - 963D:Frequency of String (bitset暴力搞)的相关文章

Codeforces 442B Kolya and Tandem Repeat(暴力)

题目连接:Codeforces 442B Kolya and Tandem Repeat 题目大意:给出一个字符串,可以再添加n个字符,问说可以找到SS的子串形式,S尽量长. 解题思路:枚举长度和起点判断即可,超过len的可以作为任意值,但是超过len+n就不行了. #include <cstdio> #include <cstring> const int N = 205; int n, len; char s[N]; bool judge (int l) { if (l <

Codeforces Round #224 (Div. 2) D 暴力搜索加记忆化

题意读了半年,英语太渣,题意是摆两个棋子在棋盘上作为起点,但是起点不能在#上,然后按照图的指示开始走, < 左 > 右 ^上 v下,走的时候只能按照图的指示走,如果前方是 #的话,可以走进去,但是 走进去之后便不能再走了,走的途中两个棋子不能相碰,但是最终都走到同一个#里是没事的,并且若是能走 无限步的话 输出 -1, 例如  > < 这样左右左右的走就能无限走,然后问你 两个棋子走的最大步数的和 一开始被输出-1给困住了,因为除了 .> <这样以外  还可以刚好形成一

Educational Codeforces Round 25 F. String Compression(kmp+dp)

题目链接:Educational Codeforces Round 25 F. String Compression 题意: 给你一个字符串,让你压缩,问压缩后最小的长度是多少. 压缩的形式为x(...)x(...)  x表示(...)这个出现的次数. 题解: 考虑dp[i]表示前i个字符压缩后的最小长度. 转移方程解释看代码,这里要用到kmp来找最小的循环节. 当然还有一种找循环节的方式就是预处理lcp,然后通过枚举循环节的方式. 这里我用的kmp找的循环节.复杂度严格n2. 1 #inclu

Codeforces 97D Robot in Basement bitset+模拟

题目链接:点击打开链接 题意: 每个点有一个机器人(.),下面是一些指令,每次发出指令(一个字母)所有机器人都会执行移动. 当机器人到E点就会离开. 若机器人前方是'#' 或者边界则停留原地.一个方格可以站多个机器人. bitset模拟.. #include <cstdio> #include <cstring> #include <algorithm> #include <bitset> using namespace std; char s[100005

codeforces 514C Watto and Mechanism (分段暴力)

codeforces 514C Watto and Mechanism (分段暴力) 题意: 给出一个包含n个单词的字典,给出m个待查询单词,如果单词在有且仅有一个字符不相同的情况下可以在字典里找到,则输出YES,否则输出NO 限制: 0 <= n,m <= 3*10^5; 总字符长度不大于6*10^5 思路: 分段暴力. 以查询单词长度为500分段: 查询单词长度<500则:采用set查询,复杂度为600*500*500=1.5*10^8 查询单词长度>500则:暴力查询,复杂度

Codeforces 56D Changing a String 编辑距离 dp

题目链接:点击打开链接 编辑距离,,== 一边dp一边记录前驱太累,,还是dp后找路径大法好 #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll int #define N 1010 char s[N], t[N]; int dp[N][N], n, m; // 0为插入 1为删除 2 3为替换 stru

codeforces 498D Unbearable Controversy of Being (暴力乱搞)

传送门:点击打开链接 题目大意: 定义下图为"damn rhombus",给定一个有向图,求出有多少个"damn rhombus". 解题思路1: 分析可以得出其实"damn rhombus"的意思就是求a->c通过2个节点中转的个数.也就是说 如果a->c中间中转了x个点,那么对于点对(a,c)来说"damn rhombus"就有C(x,2)个. 那么通过层数为2的bfs就可以得出答案. #include <

Codeforces Gym 100002 C &quot;Cricket Field&quot; 暴力

"Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100002 Description Once upon a time there was a greedy King who ordered his chief Architect to build a field for royal cricket inside his park. The King was so

Codeforces 425A Sereja and Swaps(暴力枚举)

题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内,然后找区间外假设有更大的值就替换掉. 求出每一个区间的最大值,最后记录下全部区间的最大值 代码: By lab104_yifan, contest: Codeforces Round #243 (Div. 2), problem: (C) Sereja and Swaps, Accepted, #