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
aaaaa
5
3 a
3 aa
2 aaa
3 aaaa
1 aaaaa
Output
3
4
4
-1
5
Input
abbb
7
4 b
1 ab
3 bb
1 abb
2 bbb
1 a
2 abbb
Output
-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暴力搞)的更多相关文章

  1. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  2. cf914F. Substrings in a String(bitset 字符串匹配)

    题意 题目链接 Sol Orz jry 和上一个题一个思路吧,直接bitset乱搞,不同的是这次有了修改操作 因为每次修改只会改两个位置,直接暴力改就好了 #include<bits/stdc+ ...

  3. Frequency of String CodeForces - 963D

    http://codeforces.com/contest/963/problem/D 题解:https://www.cnblogs.com/Blue233333/p/8881614.html 记M为 ...

  4. 【CodeForces】914 F. Substrings in a String bitset

    [题目]F. Substrings in a String [题意]给定小写字母字符串s,支持两种操作:1.修改某个位置的字符,2.给定字符串y,查询区间[l,r]内出现y多少次.|s|,Σ|y|&l ...

  5. Codeforces 34C-Page Numbers(set+vector+暴力乱搞)

    C. Page Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. 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 ...

  7. Codeforces Gym 100002 C "Cricket Field" 暴力

    "Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1000 ...

  8. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  9. HDU 5506:GT and set bitset+暴力

    GT and set  Accepts: 35  Submissions: 194  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 655 ...

随机推荐

  1. Python面试题之Python正则表达式指南

    1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...

  2. How to create a notification with NotificationCompat.Builder?AAAA

    Ask Question up vote 49 down vote favorite 19 I need to create a simple notification which will be s ...

  3. ubuntu16.04 虚拟机 安装win7/win10

    http://www.xitongcheng.com/jiaocheng/xtazjc_article_26588.html https://blog.csdn.net/sunyao_123/arti ...

  4. centos/linux扩容Swap分区

    查看现在的swap容量 [root@node1 ~]# free -h total used free shared buff/cache available Mem: 15G 3.8G 2.1G 5 ...

  5. Intellij IDEA生成serialVersionUID

    默认情况下Intellij IDEA不会提示继承了Serializable接口的类生成serialVersionUID的警告.如果需要生成serialVersionUID,就要在Preferences ...

  6. scala mapPartitionsWithIndex函数的使用

    var rdd1=sc.makeRDD(Array((1,"A"),(2,"B"),(3,"C"),(4,"D")),2 ...

  7. Windows安装Ubuntu桌面操作系统到移动硬盘中以及错误解决

    用到的工具:U盘一个(usb3.0,你懂的),移动硬盘(我这个是笔记本里面取出来的机械硬盘装上的盒子) 第一步:下载Ubuntu系统iso镜像文件 下载Ubuntu系统iso镜像文件,由于我是新手,下 ...

  8. java-二维数组——with 刘童格

    #include<iostream> #include<string> using namespace std; void MaxIntArray(int a[],int &a ...

  9. 报错如HTTP Status 404 - /ssh_crm/jsp/linkman/add.jsp/

    明显是写错了, HTTP Status 404 - /ssh_crm/jsp/linkman/add.jsp/ 应该改成 HTTP Status 404 - /ssh_crm/jsp/linkman/ ...

  10. mysql数据库优化课程---9、php用什么写的

    mysql数据库优化课程---9.php用什么写的 一.总结 一句话总结:php是用c语言写的,所以php里面的那些模块什么都是c语言 c 1.php用什么写的? c php是用c语言写的,所以php ...