L-Gap Substrings(uva 10829)
题意:有一种形如uvu形式的字符串,其中u是非空字符串,且V的长度正好为L,那么称这个字符串为L-Gap字符串
给出一个字符串S,以及一个正整数L,问S中有多少个L-Gap子串.
/*
这道题用到一个常用的技巧(虽然我这是第一次见)。
枚举U的长度,然后每隔U枚举i,然后确定j,则j一定在另一个U里面,有了i、j之后,向左向右扩展,看看能匹配多长。
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#define N 600010
using namespace std;
int t1[N],t2[N],c[N],s[N],sa[N],rk[N],height[N],n;
int st[N][],lg2[N];
char ch[N];
bool cmp(int *y,int a,int b,int k){
int a1=y[a],b1=y[b];
int a2=a+k<=n?y[a+k]:-;
int b2=b+k<=n?y[b+k]:-;
return a1==b1&&a2==b2;
}
void DA(){
int *x=t1,*y=t2,m=;
for(int i=;i<=m;i++) c[i]=;
for(int i=;i<=n;i++) c[x[i]=s[i]]++;
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=n;i;i--) sa[c[x[i]]--]=i;
for(int k=,p=;k<=n;k*=,m=p,p=){
for(int i=n-k+;i<=n;i++) y[++p]=i;
for(int i=;i<=n;i++) if(sa[i]>k) y[++p]=sa[i]-k;
for(int i=;i<=m;i++) c[i]=;
for(int i=;i<=n;i++) c[x[y[i]]]++;
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=n;i;i--) sa[c[x[y[i]]]--]=y[i];
swap(x,y);p=;x[sa[]]=;
for(int i=;i<=n;i++)
if(cmp(y,sa[i-],sa[i],k)) x[sa[i]]=p;
else x[sa[i]]=++p;
if(p>=n) break;
}
}
void geth(){
for(int i=;i<=n;i++) rk[sa[i]]=i;
for(int i=,j=;i<=n;i++){
while(s[i+j]==s[sa[rk[i]-]+j]) j++;
height[rk[i]]=j;
if(j) j--;
}
for(int i=;i<=n;i++) lg2[i]=lg2[i>>]+;
for(int i=;i<=n;i++) st[i][]=height[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
st[i][j]=min(st[i][j-],st[i+(<<j-)][j-]);
}
int query(int l,int r){
l=rk[l];r=rk[r];
if(l>r) swap(l,r);
int k=lg2[r-l];
return min(st[l+][k],st[r-(<<k)+][k]);
}
int main(){
int T;scanf("%d",&T);
for(int cas=;cas<=T;cas++){
long long ans=;int G;
scanf("%d%s",&G,ch+);
int len=strlen(ch+);
for(int i=;i<=len;i++) s[i]=s[len*-i+]=ch[i]-'a'+;
s[len+]=;n=len*+;
DA();geth();
for(int U=;U*+G<=len;U++)
for(int i=;i+U+G<=len;i+=U){
int j=i+U+G;
if(s[i]!=s[j]) continue;
int L=min(U,query(i,j))+min(U,query(len*-i+,len*-j+))-;//分别为向右和向左扩展
if(L>=U) ans+=L-U+;
}
cout<<"Case "<<cas<<": "<<ans<<endl;
}
return ;
}
L-Gap Substrings(uva 10829)的更多相关文章
- 【SPOJ】Substrings(后缀自动机)
[SPOJ]Substrings(后缀自动机) 题面 Vjudge 题意:给定一个长度为\(len\)的串,求出长度为1~len的子串中,出现最多的出现了多少次 题解 出现次数很好处理,就是\(rig ...
- 【SPOJ】Distinct Substrings(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
- New Distinct Substrings(后缀数组)
New Distinct Substrings(后缀数组) 给定一个字符串,求不相同的子串的个数.\(n<=50005\). 显然,任何一个子串一定是后缀上的前缀.先(按套路)把后缀排好序,对于 ...
- 并查集(UVA 1106)
POINT: 把每个元素看成顶点,则一个简单化合物就是一条无向边,若存在环(即k对组合中有k种元素),则危险,不应该装箱,反之,装箱: 用一个并查集维护连通分量集合,每次得到一种化合物(x, y)时检 ...
- LeetCode算法题-Count Binary Substrings(Java实现)
这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...
- 2018.11.24 spoj New Distinct Substrings(后缀数组)
传送门 双倍经验(弱化版本) 考虑求出来heightheightheight数组之后用增量法. 也就是考虑每增加一个heightheightheight对答案产生的贡献. 算出来是∑∣S∣−heigh ...
- CSU-1632 Repeated Substrings (后缀数组)
Description String analysis often arises in applications from biology and chemistry, such as the stu ...
- 【UVA10829】 L-Gap Substrings (后缀数组)
Description If a string is in the form UVU, where U is not empty, and V has exactly L characters, we ...
随机推荐
- react的redux中间件
redux 的中间件(对dispatch功能的拦截和增强,一般不用自己写,有插件) dispath是一个方法,可以自行中间拦截更改: store.dispatch = function(action) ...
- inotifywait实时监控文件目录
一.inotify简介 inotify 是一种强大的.细粒度的.异步文件系统监控机制,它满足各种各样的文件监控需要,可以监控文件系统的访问属性.读写属性.权限属性.创建删除.移动等操作,也可以监控文件 ...
- Nginx认证
Nginx 的 ngx_http_auth_basic_module模块允许通过使用“HTTP基本认证”协议验证用户名和密码来限制对资源的访问. 配置举例: location / { auth_bas ...
- 学习Pytbon第三天,用户输入
_username ='dream' #定义用户名 _password ='dream123'#定义用户密码username = input("username:")#请输入用户名 ...
- A1009 Product of Polynomials (25)(25 分)
A1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are tw ...
- (WPF&Silverlight)silverlight自定义控件
2个半小时弄懂了自定义控件是怎么回事儿. 在silverlight中创建一个UserControl,把上面sliderbar的外观和功能都封装在里面. 以自定义控件mapslider控件为例: 1.首 ...
- loj2043 「CQOI2016」K 远点对
k-d tree 裸题------ #include <algorithm> #include <iostream> #include <cstdio> using ...
- Windows Server 笔记(七):Windows Server 2012 R2 NIC Teaming(NIC组)
什么是NIC Teaming? NIC Teaming 就是将两个或更多的网络适配器组合在一起,从而达到容错和带宽聚合作用.NIC Teaming 中的每个网络适配器都是物理存在的(虚 ...
- hnust hold不住的老师
问题 H: Hold不住的老师 时间限制: 1 Sec 内存限制: 128 MB提交: 415 解决: 63[提交][状态][讨论版] 题目描述 因为我们学校ACM集训队取得的一个个优异成绩,AC ...
- Android App程序结构
先看结构图: ====================================== 1. /src 源码目录,不解释. 2. /gen gen目录是ADT 自动生成的代码所在位置 ...