HDOJ 题目3518 Boring counting(后缀数组,求不重叠反复次数最少为2的子串种类数)
Boring counting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2253 Accepted Submission(s): 924
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”).
aaaa
ababcabb
aaaaaa
#
2
3
3
pid=3520" style="color:rgb(26,92,200); text-decoration:none">3520
3519 3521 3522Problem : 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的子串种类数)的更多相关文章
- hdu 3518 Boring counting 后缀数组LCP
题目链接 题意:给定长度为n(n <= 1000)的只含小写字母的字符串,问字符串子串不重叠出现最少两次的不同子串个数; input: aaaa ababcabb aaaaaa # output ...
- hdu 3518 Boring counting 后缀数组 height分组
题目链接 题意 对于给定的字符串,求有多少个 不重叠的子串 出现次数 \(\geq 2\). 思路 枚举子串长度 \(len\),以此作为分界值来对 \(height\) 值进行划分. 显然,对于每一 ...
- poj3261 Milk Patterns 后缀数组求可重叠的k次最长重复子串
题目链接:http://poj.org/problem?id=3261 思路: 后缀数组的很好的一道入门题目 先利用模板求出sa数组和height数组 然后二分答案(即对于可能出现的重复长度进行二分) ...
- hdu 3518 Boring counting 后缀数组
题目链接 根据height数组的性质分组计算. #include <iostream> #include <vector> #include <cstdio> #i ...
- hdu 3518 Boring counting 后缀数组基础题
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- hdu3518 Boring counting(后缀数组)
Boring counting 题目传送门 解题思路 后缀数组.枚举每种长度,对于每个字符串,记录其最大起始位置和最小起始位置,比较是否重合. 代码如下 #include <bits/stdc+ ...
- 【HDOJ】3518 Boring Counting
后缀数组2倍增可解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10 ...
- poj1743 后缀数组求不可重叠的重复出现的子串最长长度
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25348 Accepted: 8546 De ...
- poj 1743 二分答案+后缀数组 求不重叠的最长重复子串
题意:给出一串序列,求最长的theme长度 (theme:完全重叠的子序列,如1 2 3和1 2 3 or 子序列中每个元素对应的差相等,如1 2 3和7 8 9) 要是没有差相等这个条件那就好办 ...
随机推荐
- Android 低功耗蓝牙的多设备连接与数据接收,简单实现
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...
- 派遣函数IRP
派遣函数是Windows驱动程序中的重要概念.驱动程序的主要功能是负责处理I/O请求,其中大部分I/O请求是在派遣函数中处理的. 用户模式下所有对驱动程序的I/O请求,全部由操作系统转换为一个叫做IR ...
- javascript中经典继承的兼容写法
function create(obj) { // 2.1 判断浏览器支持不支持 Object.create // 如果支持,直接使用 Object.create // 如果不支持,自己实现 if(O ...
- MemCached总结三:PHP的memcached管理接口
在Web系统中应用MemCached缓存技术,必须使用客户端API(PHP)进行访问,这样才能将用户请求的动态数据,缓存到memcached服务器中,来减少对数据库的访问压力.PHP中提供了用于内存缓 ...
- axis2 1.7.1使用教程
写在前面 本文只说Axis2的用法. 1.下载与部署 需要下载两个文件: 下载地址:http://mirrors.cnnic.cn/apache/axis/axis2/java/core/1.7.1/ ...
- mysql简单语句
创建名为user的数据库: create database user; 显示所有数据库: show databases; 选择名为user的数据库: use user; 显示所有表: show tab ...
- ubuntu区域语言(locale)设置(切换为中文)
第一步:编辑文件 代码:sudo gedit /var/lib/locales/supported.d/local (这个文件是所有已经激活的区域语言的列表) 写入相关内容,比如写入如下内容: zh ...
- Java并发-J.U.C之AQS
AQS(Abstract Queue Synchronizer)介绍 [死磕Java并发]—–J.U.C之AQS(一篇就够了) 下面讲解具体的Java并发工具类 1 CountDownLatch 参考 ...
- ZooKeeper伪集群的搭建(Windows)
首先下载 zookeeper 地址:https://www.apache.org/dyn/closer.cgi/zookeeper/ 1.下载完成解压后修改文件夹名字为zookeeper1,然后删除c ...
- 前端html之------>Table实现表头固定
文章来源于:https://www.cnblogs.com/dacuotecuo/p/3657779.html,请尊重原创,转载请注明出处. 说明:这里主要实现了表头的固定和上下滚动的滑动实现:时间的 ...