题目大意:

  给一个串s和很多模式串,对每个模式串求s的一个最短的子串使得这个子串中包含至少k个该模式串。

题目分析:

  均摊分析,有sqrt(n)种长度不同的模式串,所以有关的串只有msqrt(n)种。暴力用AC自动机找出来即可。

代码:

  

 #include<bits/stdc++.h>
using namespace std; const int maxn = ;
const int sigma = ; int n,num,root,d[maxn],fa[maxn],fail[maxn],Ex[maxn];
string str,query[maxn];
vector<int> ans[maxn]; int Number(char ch){return (int)(ch-'a');} struct trie{int data,end,nxt[sigma];}T[maxn]; void insert(int now,int pla,int tnode){
if(pla == query[now].size()){T[tnode].end=now;return;}
int p = Number(query[now][pla]);
if(T[tnode].nxt[p]){insert(now,pla+,T[tnode].nxt[p]);}
else{
num++;T[tnode].nxt[p] = num;T[num].data = p;
fa[num] = tnode; insert(now,pla+,num);
}
} void read(){
cin >> str >> n;
for(int i=;i<=n;i++){
cin >> d[i] >> query[i];
insert(i,,root);
}
} queue <int> q;
void BuildAC(){
for(int i=;i<sigma;i++) if(T[root].nxt[i]) q.push(T[root].nxt[i]);
while(!q.empty()){
int k = q.front();q.pop();
int hh = fail[fa[k]];
while(hh != root){
if(T[hh].nxt[T[k].data]){fail[k]=T[hh].nxt[T[k].data];break;}
else hh = fail[hh];
}
if(T[hh].nxt[T[k].data]&&(!fail[k])&&T[hh].nxt[T[k].data]!=k){
fail[k]=T[hh].nxt[T[k].data];
}
for(int i=;i<sigma;i++) if(T[k].nxt[i]) q.push(T[k].nxt[i]);
}
} void BuildExtraFail(){
q.push(root);
while(!q.empty()){
int k = q.front();q.pop();
int hh = fail[k];
if(T[hh].end){Ex[k] = hh;}else Ex[k] = Ex[hh];
for(int i=;i<sigma;i++) if(T[k].nxt[i]) q.push(T[k].nxt[i]);
}
} void RunAC(){
int now = root;
for(int i=;i<str.length();i++){
int p = Number(str[i]);
if(T[now].nxt[p]) now = T[now].nxt[p];
else{
int hh = fail[now];
int fg = false;
while(hh != root){
if(T[hh].nxt[p]){fg=;now=T[hh].nxt[p];break;}
else hh = fail[hh];
}
if(fg == )goto loop;
if(T[hh].nxt[T[p].data])now=T[hh].nxt[T[p].data];
else now = root;
}
loop:int z = Ex[now];
if(T[now].end) ans[T[now].end].push_back(i);
while(z!=root){ans[T[z].end].push_back(i);z=Ex[z];}
}
} int Min(int a,int b){return a<b?a:b;} void work(){
BuildAC();
BuildExtraFail();
RunAC();
for(int i=;i<=n;i++){
if(ans[i].size()<d[i]){cout<<-<<endl;continue;}
int minn = ;
for(int j=d[i]-;j<ans[i].size();j++){
minn = Min(minn,ans[i][j]-ans[i][j-d[i]+]+query[i].length());
}
cout<<minn<<endl;
}
} int main(){
read();
work();
return ;
}

Codeforces963C Frequency of String 【字符串】【AC自动机】的更多相关文章

  1. 模板—字符串—AC自动机(多模式串,单文本串)

    模板—字符串—AC自动机(多模式串,单文本串) Code: #include <queue> #include <cstdio> #include <cstring> ...

  2. HDU 6096 String(AC自动机+树状数组)

    题意 给定 \(n\) 个单词,\(q\) 个询问,每个询问包含两个串 \(s_1,s_2\),询问有多少个单词以 \(s_1\) 为前缀, \(s_2\) 为后缀,前后缀不能重叠. \(1 \leq ...

  3. [coci2012]覆盖字符串 AC自动机

    给出一个长度为N的小写字母串,现在Mirko有M个若干长度为Li字符串.现在Mirko要用这M个字符串去覆盖给出的那个字符串的.覆盖时,必须保证:1.Mirko的字符串不能拆开,旋转:2.Mirko的 ...

  4. 【ZOJ 3228】Searching the String 【AC自动机】

    题意 给出n个模式串和一个文本串,输出各个模式串在文本串中出现的次数.模式串有两种类型,0类型代表可以有重叠,1类型代表不能有重叠.模式串可能出现重复. 分析 算是AC自动机的模板题? 因为模式串可以 ...

  5. HDU - 6096 :String (AC自动机,已知前后缀,匹配单词,弱数据)

    Bob has a dictionary with N words in it. Now there is a list of words in which the middle part of th ...

  6. ZOJ3228 Searching the String (AC自动机)

    Searching the String Time Limit: 7 Seconds                                      Memory Limit: 129872 ...

  7. 【bzoj1030】: [JSOI2007]文本生成器 字符串-AC自动机-DP

    [bzoj1030]: [JSOI2007]文本生成器 首先把匹配任意一个的个数的问题转化为总个数-没有一个匹配的个数 先构造AC自动机,然后枚举每一位的字母以及在自动机上的位置 f[i][j]为第i ...

  8. 【bzoj3172】: [Tjoi2013]单词 字符串-AC自动机

    [bzoj3172]: [Tjoi2013]单词 先用所有单词构造一个AC自动机 题目要求的是每个单词在这个AC自动机里匹配到的次数 每次insert一个单词的时候把路径上的cnt++ 那么点p-&g ...

  9. Substring Frequency (II) LightOJ - 1427 AC自动机

    https://vjudge.net/problem/LightOJ-1427 把所有模式串加入ac自动机,然后search的时候暴力,每个子串都暴力一下就好. 其实AC自动机就是,先建立好trie图 ...

随机推荐

  1. 作为一个.NET开发者,怎么看待和选择层出不穷的新技术,新架构?

    经常在一些技术社区看到这些的问题,一个.NET开发者去求职,看到应聘的公司的技术栈还是比较老的ASP.NET WEBFORM的时候,希望了解未来会否使用ASP.NET MVC的时候,没有获得肯定答复, ...

  2. Appium-处理系统弹窗

    前言: 最近在搞appium自动化,iOS的系统弹窗是大家都会遇到的,本文来总结处理这种弹窗的用法. 环境: MacOS:10.13.4 Appium-desktop:1.6.1 Xcode:9.3. ...

  3. 求n!中含有某个因子个数的方法

    链接 [https://www.cnblogs.com/dolphin0520/archive/2011/04/11/2012891.html]

  4. openstack-KVM管理工具

    一. virsh 通过libvirt API管理Hpervisor.node.domain,实现多数功能调用. 即统一管理多台计算机上的域. 1.管理其他服务器(node) (1)修改配置文件:vim ...

  5. Requires: libc.so.6(GLIBC_2.14)(64bit)

    centos6 - CentOS 6 - libc.so.6(GLIBC_2.14)(64bit) is needed by - Server Faulthttps://serverfault.com ...

  6. 关于JS动画和CSS3动画的性能差异

    本文章为综合其它资料所得. 根据Google Developer,Chromium项目里,渲染线程分为main thread和compositor thread. 如果CSS动画只是改变transfo ...

  7. If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.

    学习Spring Boot 过程中遇到了下列这个问题 Description: Failed to configure a DataSource: 'url' attribute is not spe ...

  8. ubuntu18.04 安装 php7.2

    sudo apt-get install software-properties-common python-software-properties sudo add-apt-repository p ...

  9. java学习之—数组的曾删改查

    /** * 数组的曾删改查 * Create by Administrator * 2018/6/8 0008 * 上午 9:54 **/ public class HighArray { priva ...

  10. DAY08、文件操作

    一.文件操作模式汇总: 主模式: r:读模式 w:写模式(无创建,有清空) a:追加(有创建的功能) x:写,必须自己创建文件,否则报错 从模式: t:文本操作(默认模式)r >rt,w> ...