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. 使用Azure Docker容器注册表服务

    1.登录你的Azure 容器注册表服务 az acr login --name ledesign 2.给你本地准备好的Image打上上云之前的版本标签 docker tag ledesign-serv ...

  2. GStreamer基础教程02 - 基本概念

    摘要 在 Gstreamer基础教程01 - Hello World中,我们介绍了如何快速的通过一个字符串创建一个简单的pipeline.为了能够更好的控制pipline中的element,我们需要单 ...

  3. LeetCode Weekly Contest 20

    1. 520. Detect Capital 题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断.还有:我感觉自己写的代码很丑,判断条件比较多,需要改进 ...

  4. Docker 内部之间的网络连接

    一.简介 内部网络连接的2中方式: Docker NetWorking (1.9版本之后推荐使用这个)和 Docker link(1.9 版本之前都使用这个) 推荐使用docker networkin ...

  5. SQLServer 行转列,统计,二次分组

    create table test (   bizdate datetime, --日期   classes varchar(50), --班次 '白班' 或 '夜班'   qty int --产量 ...

  6. hdu1317 XYZZY Floyd + Bellman_Ford

    这题,我在学搜索的时候做过.不过好像不叫这名字. 1.先用Floyd算法判断图的连通性.如果1与n是不连通的,输出hopeless. 2.用Bellman_Ford算法判断是否有正圈,如果某点有正圈, ...

  7. mysql数据库索引原理及其常用引擎对比

    索引原理 树数据结构及其算法简介 B+/-树: - 多路搜索树; - 时间复杂度O(logdN);h为节点出度,d为深度 红黑树: - 节点带有颜色的平衡二叉树 - 时间复杂度O(log2N);h节点 ...

  8. 运行于Windows内建账户下的服务访问映射网络驱动器的方法

    Windows服务如果是运行在本地系统帐户下或本地服务帐户下,它只能访问这个账户自己创建的映射驱动器,UNC路径访问则不受次限制. LocalService Account (NT AUTHORITY ...

  9. 定时器篇---java.util.TimerTask和quartz

    最近项目中出现了定时执行任务的东西,研究了一下,觉得挺不错的,以后还用得到,就总结了下. 这里只介绍两种java.util.Timer 和 quartz java.util.Timer java自带的 ...

  10. js页面传值,cookie

    // 获取页面穿值 function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + " ...