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两两不同最多只有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的更多相关文章
- 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 解 对于题目要求,显然一开始的树,要求度数为偶数的节点个数为奇数个,通过奇 ...
- 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 ...
- Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)
A. Alternating Sum 就是个等比数列,特判公比为 $1$ 的情况即可. #include <bits/stdc++.h> using namespace std; ; ; ...
- CodeForces 837D - Round Subset | Educational Codeforces Round 26
/* CodeForces 837D - Round Subset [ DP ] | Educational Codeforces Round 26 题意: 选k个数相乘让末尾0最多 分析: 第i个数 ...
- 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 ----------------------------------------------------------我是分 ...
- 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 ...
- Divide by Zero 2018 and Codeforces Round #474 (Div. 1 + Div. 2, combined)
思路:把边看成点,然后每条边只能从下面的边转移过来,我们将边按照u为第一关键字,w为第二关键字排序,这样就能用线段树维护啦. #include<bits/stdc++.h> #define ...
- 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 ...
- 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 ...
随机推荐
- jQuery 学习笔记(5)(事件绑定与解绑、事件冒泡与事件默认行为、事件的自动触发、自定义事件、事件命名空间、事件委托、移入移出事件)
1.事件绑定: .eventName(fn) //编码效率略高,但部分事件jQuery没有实现 .on(eventName, fn) //编码效率略低,所有事件均可以添加 注意点:可以同时添加多个相同 ...
- echart tootip使用技巧
1.关于混合折线图tooltip显示不同单位 formatter: function (params){ return params[0].name + '<br/>' + params ...
- cocos2d-x在Android上的编译过程(3):简化Android.mk文件的编写
在编译动态库时.要求我们要去编写jni/Android.mk文件.告诉编译器编译出来的库时应包括包括编译文件和其它引用库.但对于一个大项目来说,维护这个文件肯定是一件比較繁琐的事情.由于每加一个文件或 ...
- Ngnix 配置文件
配置文件路径/usr/local/nginx/conf/nginx.conf user www www; #nginx 服务的伪用户和用户组 worker_processes auto; #启动进程, ...
- win7共享打印机和防火墙配置
今天给公司一台Win7电脑连接的打印机做共享.办公司共6台电脑,其中1台是连接了打印机,并安装了打印机驱动,可以正常本机使用打印机.现在需要其他5台电脑也共享使用打印机. 1.当共享的时候,提示“无法 ...
- java中的ElasticSearch搜索引擎介绍。
ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...
- app 开发
移动APP开发 ios Android 中国人写的 MUI 布局框架 HTML5plus 硬件驱动调用(打开摄像头,闪光灯,震动) 和 系统调用(打开相册,通讯录,message) http:/ ...
- Navigation包中的move_base和amcl实现自动驾驶
安装功能包: 1.安装导航定位包navigation $ sudo apt-get install ros-indigo-navigation 2.由于导航包在/cmd_val下发布的移动数据加速度会 ...
- C#中抽象类和接口的区别与使用
一.抽象类: 抽象类是特殊的类,只是不能被实例化:除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法,这是普通类所不能的.抽象方法只能声明于抽象类中,且不包含任何实现,派生类必须覆盖它们.另外 ...
- binlog的原理