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. Linux Shell Scripting Cookbook 读书笔记 2

    cat,script,find, xargs, tr, tmp文件,字符串截取,批量文件重命名,固定大小文件,自动化交互 1. cat的用法 压缩连续的空白行 cat -s file 也可以用tr,将 ...

  2. 【Linux】YUM Repositories for CentOS, RHEL & Fedora Systems

    这里是官方wiki:https://wiki.centos.org/AdditionalResources/Repositories 一.简介 YUM(Yellowdog Updater Modifi ...

  3. website robots.txt 防爬虫 措施

    robots.txt文件用法举例: 1. 允许所有的robot访问 User-agent: * Allow: / 或者 User-agent: * Disallow: 2. 禁止所有搜索引擎访问网站的 ...

  4. FAQ: SBS 2011. The Windows SBS Manager service terminated unexpectedly

    Symptoms The Windows SBS Manager service is stopped with EventID 7034 every half an hour on SBS 2011 ...

  5. 读书笔记「Python编程:从入门到实践」_2.变量和简单数据类型

    做了大半年RPA了,用的工具是Kapow. 工作没有那么忙,不想就这么荒废着,想学点什么.就Python吧. 为期三个月,希望能坚持下来. 2.1 变量的命名和使用 变量名只能包含字母.数字和下划线. ...

  6. 优动漫PAINT漫画和插画方面软件特色

    优动漫PAINT也就是我们常说的clip studio paint(CSP)的中文版本,它是一款功能强大的漫画.插画绘制软件,它搭载了绘制漫画和插画所需的所有功能,包括丰富的笔工具.超强的笔压感应和手 ...

  7. 团体程序设计天梯赛-练习集-L1-025. 正整数A+B

    L1-025. 正整数A+B 本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B,其间以空 ...

  8. vc++图像保存,重绘

    新建mfc应用程序,单文档 增加绘图 分别增加命令响应 添加成员变量UINIT 图形可以运行,如何保存呢?(一个集合类,CPtArt) 用一个类的对象来保存一个图形的三个要素 所以插入一个新的类(通常 ...

  9. 容器化haproxy+keepalived

    # 拉取haproxy镜像 docker pull haproxy:1.7.8-alpine mkdir /etc/haproxy cat >/etc/haproxy/haproxy.cfg&l ...

  10. 喵哈哈村的魔法考试 Round #3 (Div.2)

    菜的抠脚 A 题解:判断能否构成一个三角形. #include "iostream" #include "algorithm" #include "c ...