Description

If a string is in the form UVU, where U is not empty, and V has exactly L characters, we say UVU
is an L-Gap string. For example, abcbabc is a 1-Gap string. xyxyxyxyxy is both a 2-Gap string
and also a 6-Gap string, but not a 10-Gap string (because U is non-empty).
Given a string s, and a positive integer g, you are to find the number of g-Gap substrings in s. s
contains lower-case letters only, and has at most 50,000 characters.

Input
The first line contains a single integer t (1 ≤ t ≤ 10), the number of test cases. Each of the t followings
contains an integer g (1 ≤ g ≤ 10) followed by a string s.

Output
For each test case, print the case number and the number of g-Gap substrings. Look at the output for
sample input for details.

Sample Input
2
1 bbaabaaaaa
5 abxxxxxab

Sample Output
Case 1: 7
Case 2: 1

【题意】

  UVU形式的串的个数,V的长度规定,U要一样,位置不一样即为不同字串

【分析】

  表示做了poj3693还是不会做这题。

  为什么会想到枚举L然后分块呢????

  为什么呢????

  这种方法于我而言还是有点难理解的啊。

  主要是分块!!

  

任意一个满足条件的UVU,假设U的长度是len,那么左端的U必然包含按照len切分的T串的某个字串,及0,len,2len,3len...。(这点要仔细想清楚)

那么枚举每个端点i*len,然后利用RMQ求后缀i*len和后缀i*len+L+len的LCP,然后字符串T反向,再求一遍反向的LCP2。(其中LCP要小于等于len,否则会重复,仔细想清楚)

最后累加求和sum+=(LCP+LCP2-1)-len+1。(这点想清楚为什么是-len)

blog:http://blog.csdn.net/u011526463/article/details/14000693

  

  还有,其实,貌似不用后缀数组直接两个while前后就可以了。时间貌似还是nlogn的。  【(2017-03-24 14:43:33)许多年发现以前在搞笑。。

  

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxl 100010 int l,len;
char s[Maxl];
int c[Maxl],cl; int mymin(int x,int y) {return x<y?x:y;} void init()
{
scanf("%d%s",&l,s);
len=strlen(s);
cl=;
for(int i=;i<len;i++) c[++cl]=s[i]-'a'+;
} int sa[Maxl],rk[Maxl],y[Maxl],wr[Maxl],Rs[Maxl];
void get_sa(int m)
{
memcpy(rk,c,sizeof(rk));
for(int i=;i<=m;i++) Rs[i]=;
for(int i=;i<=cl;i++) Rs[rk[i]]++;
for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
for(int i=cl;i>=;i--) sa[Rs[rk[i]]--]=i; int ln=,p=;
while(p<cl)
{
int k=;
for(int i=cl-ln+;i<=cl;i++) y[++k]=i;
for(int i=;i<=cl;i++) if(sa[i]>ln) y[++k]=sa[i]-ln;
for(int i=;i<=cl;i++) wr[i]=rk[y[i]]; for(int i=;i<=m;i++) Rs[i]=;
for(int i=;i<=cl;i++) Rs[wr[i]]++;
for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
for(int i=cl;i>=;i--) sa[Rs[wr[i]]--]=y[i]; for(int i=;i<=cl;i++) wr[i]=rk[i];
for(int i=cl+;i<=cl+ln;i++) wr[i]=;
p=,rk[sa[]]=;
for(int i=;i<=cl;i++)
{
if(wr[sa[i]]!=wr[sa[i-]]||wr[sa[i]+ln]!=wr[sa[i-]+ln]) p++;
rk[sa[i]]=p;
}
ln*=,m=p;
}
sa[]=rk[]=;
} int height[Maxl];
void get_he()
{
int k=;
for(int i=;i<=cl;i++) if(rk[i]!=)
{
int j=sa[rk[i]-];
if(k) k--;
while(c[i+k]==c[j+k]&&i+k<=cl&&j+k<=cl) k++;
height[rk[i]]=k;
}
} int d[Maxl][];
void rmq_init()
{
for(int i=;i<=cl;i++) d[i][]=height[i];
for(int j=;(<<j)<=cl;j++)
for(int i=;i+(<<j)-<=cl;i++)
d[i][j]=mymin(d[i][j-],d[i+(<<j-)][j-]);
} int rmq(int x,int y)
{
int t;x=rk[x];y=rk[y];
if(x>y) t=x,x=y,y=t;
x++;
int k=;
while((<<k+)<=y-x+) k++;
return mymin(d[x][k],d[y-(<<k)+][k]);
} void ffind()
{
int ans=;
for(int i=;i<=cl;i++)
{
for(int j=;j<=cl/i;j++)
{
int now=j*i+,x,y=;
if(c[now]!=c[now+l+i]||now+l+i>cl) continue;
x=mymin(i,rmq(now,now+l+i));//向后匹配
while(c[now-y-]==c[now+l+i-y-]&&y+<i) y++;//向前匹配
if(x+y-i+>) ans+=x+y-i+;
}
}
printf("%d\n",ans);
} int main()
{
int T,kase=;
scanf("%d",&T);
while(T--)
{
init();
get_sa();
get_he();
rmq_init();
printf("Case %d: ",++kase);
ffind();
}
return ;
}

[UVA10829]

2016-07-19 15:44:08


这道题我看了100遍!!

现在又明白了一点了ORZ。。。分块屌ORZ。。。

好吧,是按照u的长度分块!!

为什么呢,目的是:答案串的u至少包含一个分割点(上面的蓝色突起)

我们对于其包含的第一个分割点时计算他!!!(就是上面红色圈起的部分)

如果匹配长度越过第二个分割点,那么是会重复计算的,所以在这一题,向前匹配和向后匹配都可以直接while,越过分割点的时候就结束。

ORZ。。。

2016-11-13 14:52:25

【UVA10829】 L-Gap Substrings (后缀数组)的更多相关文章

  1. UVALive - 6869 Repeated Substrings 后缀数组

    题目链接: http://acm.hust.edu.cn/vjudge/problem/113725 Repeated Substrings Time Limit: 3000MS 样例 sample ...

  2. POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数

    题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K ...

  3. POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串

    题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS   Memory Limit: 10000K Total ...

  4. SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数

    题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...

  5. SPOJ- Distinct Substrings(后缀数组&后缀自动机)

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  6. POJ 3415 Common Substrings 后缀数组+并查集

    后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height   求完之后按height值从大往小合并.  height值代表的是  sa[i]和sa[i ...

  7. POJ1226:Substrings(后缀数组)

    Description You are given a number of case-sensitive strings of alphabetic characters, find the larg ...

  8. SPOJ DISUBSTR Distinct Substrings 后缀数组

    题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #in ...

  9. POJ-Common Substrings(后缀数组-长度不小于 k 的公共子串的个数)

    题意: 长度不小于 k 的公共子串的个数 分析: 基本思路是计算 A 的所有后缀和 B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于 k 的部分全部加起来. 先将两个字符串连起来,中间 ...

  10. spoj-694-Distinct Substrings(后缀数组)

    题意: 给定一个字符串,求不相同的子串的个数 分析: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同 的 前 缀 的 个 数 . 如 果 所 有 的 后 缀 按 照 suffix ...

随机推荐

  1. How and Why Unsafe is Used in Java---reference

    By Peter Lawrey https://www.voxxed.com/blog/2014/12/how-and-why-unsafe-is-used-in-java/ Overview sun ...

  2. GUI编程笔记(java)08:GUI通过鼠标移动到按钮上更改背景色案例

    首先我们看看源代码如下: package cn.itcast_06; import java.awt.Button; import java.awt.Color; import java.awt.Fl ...

  3. 30种mysql优化sql语句查询的方法<转>

    转自百度文库 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引.   2.应尽量避免在 where 子句中使用!=或<>操作符,否 ...

  4. DNS服务器安装配置案例详解

    案例配置要求:假设有一个域名:tianyik.com主机为:192.168.31.36    mail 192.168.31.37    www  192.168.31.38    pop --> ...

  5. PHP 进制问题

    PHP有一些函数提供进制之间的转化问题 在Math函数中有一部分: decbin() - 十进制转换为二进制 bindec() — 二进制转换为十进制 octdec() - 八进制转换为十进制 hex ...

  6. PHP 时间函数集合

    计算指定日期的前几天,几个月或者几年的函数 $a = '2014/08/21';echo date( "Y-m-d", strtotime( "-6 month  &qu ...

  7. 一个实例明白AutoResetEvent和 ManulResetEvent的用法

    先看一段代码: public class WaitHandlerExample {                 public static AutoResetEvent waitHandler;  ...

  8. java_泛型(2016-11-17)

    没有自己敲,这篇博客讲的不错,直接记录. 犯懒啊 重点关注 T,?以及擦除 Java总结篇系列:Java泛型:http://www.cnblogs.com/lwbqqyumidi/p/3837629. ...

  9. ScheduleThreadPoolExecutor源码分析

    ScheduleThreadPoolExecutor源码分析(一) Java中ScheduleThreadPoolExecutor主要用于执行延迟任务或者按照一定的频率执行任务.其中scheduleA ...

  10. 编写类String的构造函数、拷贝构造函数、析构函数和赋值函数

    一.题目: class String { public: String(const char *str = NULL); // 普通构造函数 String(const String &othe ...