题意:有一个串s,n个串模式串t,问s的子串中长度最小的包含t k次的长度是多少

题解:把所有t建ac自动机,把s在ac自动机上匹配.保存每个模式串在s中出现的位置.这里由于t两两不同最多只有xsqrt(x),x是总长度.然后双指针扫一遍即可

这里有一个很重要的优化技巧,由于ac自动机上不是每个点都是t的结尾,我们把fail向上跳一次变成直接跳到t的结尾,build预处理一下

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const ull ba=233;
const db eps=1e-7;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=100000+10,inf=0x3f3f3f3f; char s[N],p[N];
vi v[N];
struct ACM{
int root,tot;
int ch[N][26],fail[N],id[N],go[N];
int newnode()
{
memset(ch[tot],-1,sizeof ch[tot]);
id[tot]=0;
return tot++;
}
void init(){tot=0;root=newnode();}
void ins(int x)
{
int now=root,n=strlen(s);
for(int i=0;i<n;i++)
{
if(ch[now][s[i]-'a']==-1)ch[now][s[i]-'a']=newnode();
now=ch[now][s[i]-'a'];
}
id[now]=x;
}
void build()
{
queue<int>q;
fail[root]=root;
for(int i=0;i<26;i++)
{
if(ch[root][i]==-1)ch[root][i]=root;
else
{
fail[ch[root][i]]=root;
q.push(ch[root][i]);
}
}
while(!q.empty())
{
int now=q.front();q.pop();
if(id[fail[now]])go[now]=fail[now];
else go[now]=go[fail[now]];
for(int i=0;i<26;i++)
{
if(ch[now][i]==-1)ch[now][i]=ch[fail[now]][i];
else
{
fail[ch[now][i]]=ch[fail[now]][i];
q.push(ch[now][i]);
}
}
}
}
void cal()
{
int now=root;
for(int i=0;p[i];i++)
{
now=ch[now][p[i]-'a'];
int te=now;
while(te!=root)
{
if(id[te])v[id[te]].pb(i);
te=go[te];
}
}
}
}ac;
int a[N],sz[N];
int main()
{
int n;
scanf("%s%d",p,&n);
ac.init();
for(int i=1;i<=n;i++)
{
scanf("%d%s",&a[i],s);
sz[i]=strlen(s);
ac.ins(i);
}
ac.build();
ac.cal();
for(int i=1;i<=n;i++)
{
if(v[i].size()<a[i])puts("-1");
else
{
// for(int x:v[i])printf("%d ",x);puts("");
int ans=inf;
for(int j=0;j+a[i]-1<v[i].size();j++)
{
ans=min(ans,v[i][j+a[i]-1]-v[i][j]+sz[i]);
}
printf("%d\n",ans);
}
}
return 0;
}
/******************** ********************/

Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)D. Frequency of String的更多相关文章

  1. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1) 963B 964D B Destruction of a Tree

    题 OvO http://codeforces.com/contest/963/problem/B CF 963B 964D 解 对于题目要求,显然一开始的树,要求度数为偶数的节点个数为奇数个,通过奇 ...

  2. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2)

    A. Splits time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  3. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)

    A. Alternating Sum 就是个等比数列,特判公比为 $1$ 的情况即可. #include <bits/stdc++.h> using namespace std; ; ; ...

  4. CodeForces 837D - Round Subset | Educational Codeforces Round 26

    /* CodeForces 837D - Round Subset [ DP ] | Educational Codeforces Round 26 题意: 选k个数相乘让末尾0最多 分析: 第i个数 ...

  5. Codeforces 932 A.Palindromic Supersequence (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    占坑,明天写,想把D补出来一起写.2/20/2018 11:17:00 PM ----------------------------------------------------------我是分 ...

  6. ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A

    2018-02-19 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 mega ...

  7. Divide by Zero 2018 and Codeforces Round #474 (Div. 1 + Div. 2, combined)

    思路:把边看成点,然后每条边只能从下面的边转移过来,我们将边按照u为第一关键字,w为第二关键字排序,这样就能用线段树维护啦. #include<bits/stdc++.h> #define ...

  8. ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined)

    靠这把上了蓝 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 megabyte ...

  9. Codeforces 932 C.Permutation Cycle-数学 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    C. Permutation Cycle   time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

随机推荐

  1. IE浏览器Web自动化

    一.常见问题 1.1  在IE11浏览器下运行自动化脚本特别缓慢. 具体表现:(64位IE驱动器下)脚本运行慢,尤其是文本框输入,其它浏览器是一起输入,但IE是单个字节输入字符串 解决方法:更换IE的 ...

  2. 探究Java中的锁

    一.锁的作用和比较 1.Lock接口及其类图 Lock接口:是Java提供的用来控制多个线程访问共享资源的方式. ReentrantLock:Lock的实现类,提供了可重入的加锁语义 ReadWrit ...

  3. vue-property-decorator 提供 OO 的风格 Vue Component 方便类型声明

    @Prop  父子组件之间传值 Install: npm install --save vue-property-decorator Child: <template> <div&g ...

  4. generatorConfiguration详解

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration ...

  5. 四、latex字体字号设置

    latex的思想是格式与内容的分离,所以不建议在文中使用大量命令,而是定义一个新的命令

  6. sql 身份证计算年龄和性别

    IdentityNumber 是身份证号 年龄: ,), GETDATE()) / 365.25) as '推荐人年龄', 15位的身份证计算年龄: case when b.IdentityNumbe ...

  7. Linux文件检索

    title: Linux文件检索 date: 2017-12-11 19:03:01 tags: linux categories: linux whereis 只要执行 whereis ls 就可以 ...

  8. git help 机器翻译

    该篇发布仅为博主个人保存并参考,内容可能不对 usage: git [--version] [--help] [-C <path>] [-c <name>=<value& ...

  9. 【Spark-core学习之四】 Spark任务提交

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...

  10. ArrayList 除重

    看到一段简洁的 ArrayList 除重代码: protected final <T> List<T> removeDuplicates(List<T> list) ...