Boring counting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2253    Accepted Submission(s): 924

Problem Description
035 now faced a tough problem,his english teacher gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover,such apearances can not overlap each other.

Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.since we can get “aaa” from [0-2](The position of string begins with 0) and [1-3]. But the interval [0-2] and
[1-3] overlaps each other.So “aaa” can not take into account.Therefore,the answer is 2(“a”,and “aa”).
 
Input
The input data consist with several test cases.The input ends with a line “#”.each test case contain a string consists with lower letter,the length n won’t exceed 1000(n <= 1000).
 
Output
For each test case output an integer ans,which represent the answer for the test case.you’d better use int64 to avoid unnecessary trouble.
 
Sample Input
aaaa
ababcabb
aaaaaa
#
 
Sample Output
2
3
3
 
Source
 
Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  3517 

pid=3520" style="color:rgb(26,92,200); text-decoration:none">3520 3519 3521 3522 

 

Problem : 3518 ( Boring counting )     Judge Status : Accepted
RunId : 14564325 Language : C++ Author : lwj1994
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta

ac代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int s[2002];
char str[2002];
int sa[2002],t1[2002],t2[2002],c[2002];
int Rank[2002],height[2002],ans;
void build_sa(int s[],int n,int m)
{
int i,j,p,*x=t1,*y=t2;
for(i=0;i<m;i++)
c[i]=0;
for(i=0;i<n;i++)
c[x[i]=s[i]]++;
for(i=1;i<m;i++)
c[i]+=c[i-1];
for(i=n-1;i>=0;i--)
sa[--c[x[i]]]=i;
for(j=1;j<=n;j<<=1)
{
p=0;
for(i=n-j;i<n;i++)
y[p++]=i;
for(i=0;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;
for(i=0;i<m;i++)
c[i]=0;
for(i=0;i<n;i++)
c[x[y[i]]]++;
for(i=1;i<m;i++)
c[i]+=c[i-1];
for(i=n-1;i>=0;i--)
sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1;
x[sa[0]]=0;
for(i=1;i<n;i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
if(p>=n)
break;
m=p;
}
}
void getHeight(int s[],int n)
{
int i,j,k=0;
for(i=0;i<=n;i++)
Rank[sa[i]]=i;
for(i=0;i<n;i++)
{
if(k)
k--;
j=sa[Rank[i]-1];
while(s[i+k]==s[j+k])
k++;
height[Rank[i]]=k;
}
}
int judge(int n,int len)
{
int maxn=sa[0],minn=sa[0],ans=0;
int i,j;
for(i=1;i<=n;i++)
{
if(height[i]<len)
{
if(maxn-minn>=len)
ans++;
maxn=minn=sa[i];
}
else
{
if(maxn<sa[i])
maxn=sa[i];
if(minn>sa[i])
minn=sa[i];
}
}
if(maxn-minn>=len)
ans++;
return ans;
}
int main()
{
int n;
while(scanf("%s",str)!=EOF)
{
int i;
if(strcmp(str,"#")==0)
break;
int len=strlen(str);
for(i=0;i<len;i++)
s[i]=str[i]-'a'+1;
s[len]=0;
build_sa(s,len+1,30);
getHeight(s,len);
int l=0,r=len;
ans=0;
for(i=1;i<=len/2;i++)
{
ans+=judge(len,i); }
printf("%d\n",ans);
}
}

HDOJ 题目3518 Boring counting(后缀数组,求不重叠反复次数最少为2的子串种类数)的更多相关文章

  1. hdu 3518 Boring counting 后缀数组LCP

    题目链接 题意:给定长度为n(n <= 1000)的只含小写字母的字符串,问字符串子串不重叠出现最少两次的不同子串个数; input: aaaa ababcabb aaaaaa # output ...

  2. hdu 3518 Boring counting 后缀数组 height分组

    题目链接 题意 对于给定的字符串,求有多少个 不重叠的子串 出现次数 \(\geq 2\). 思路 枚举子串长度 \(len\),以此作为分界值来对 \(height\) 值进行划分. 显然,对于每一 ...

  3. poj3261 Milk Patterns 后缀数组求可重叠的k次最长重复子串

    题目链接:http://poj.org/problem?id=3261 思路: 后缀数组的很好的一道入门题目 先利用模板求出sa数组和height数组 然后二分答案(即对于可能出现的重复长度进行二分) ...

  4. hdu 3518 Boring counting 后缀数组

    题目链接 根据height数组的性质分组计算. #include <iostream> #include <vector> #include <cstdio> #i ...

  5. hdu 3518 Boring counting 后缀数组基础题

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  6. hdu3518 Boring counting(后缀数组)

    Boring counting 题目传送门 解题思路 后缀数组.枚举每种长度,对于每个字符串,记录其最大起始位置和最小起始位置,比较是否重合. 代码如下 #include <bits/stdc+ ...

  7. 【HDOJ】3518 Boring Counting

    后缀数组2倍增可解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10 ...

  8. poj1743 后缀数组求不可重叠的重复出现的子串最长长度

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25348   Accepted: 8546 De ...

  9. poj 1743 二分答案+后缀数组 求不重叠的最长重复子串

    题意:给出一串序列,求最长的theme长度 (theme:完全重叠的子序列,如1 2 3和1 2 3  or  子序列中每个元素对应的差相等,如1 2 3和7 8 9) 要是没有差相等这个条件那就好办 ...

随机推荐

  1. centos6.6--------正向DNS配置

    一.正向区: 将域名解析为IP====================================================================================注 ...

  2. [Offer收割]编程练习赛42

    对局匹配 直接贪心 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #i ...

  3. 利用JavaScript制作计算器

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  4. jQuery操作DOM知识总结

    jquery操作DOM(节点) 1.创建元素 //$(htmlStr) //htmlStr:html格式的字符串 $("<span>这是一个span元素</span> ...

  5. 时间&物质&效率

    由于我的家庭是地道的农民家庭,在上学的时候,父母很辛苦的供我读初中,高中,大学. 现在我想说的是,用时间来换取效率是我求学时最大的遗憾. 举一个例子吧:每次回家坐火车,火车很费时间,假如我不缺钱,完全 ...

  6. 计算机二级考试Access教程

    本教程对编程语言各种要点进行详细的讲解介绍,从基础知识到实用技术功能,内容涵盖了从数组,类等基本概念到多态.模板等高级概念.教程本着实用的原则,每一小节都结合了可以笔试.面试的常见程序实例,以便从第一 ...

  7. 「JavaSE 重新出发」05.03.03 使用反射编写泛型数组代码

    Employee[] a = new Employee[100]; // ... // array is full a = Arrays.copyOf(a, 2 * a.length); 如何编写这样 ...

  8. apicloud开发方法。

    1.前端布局 window frame 子窗口 franmegroup  子窗口组. 一个页面比如有一个固定的顶部,然后中间区域是商品或者是什么内容,那么这个整体就是一个window,那么中间的就是i ...

  9. 遍历及过滤 first(), last() 和 eq() filter() 和 not()

    三个最基本的过滤方法是:first(), last() 和 eq(),它们允许您基于其在一组元素中的位置来选择一个特定的元素.其他过滤方法,比如 filter() 和 not() 允许您选取匹配或不匹 ...

  10. day27-1 numpy模块

    目录 numpy array 一维数组 二维数组(用的最多) np.array和list的区别 获取多维数组的行和列 多维数组的索引 高级功能 多维数组的元素替换 多维数组的合并 通过函数方法创建多维 ...