【hdu3518】Boring counting
题意:找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠)。
后缀数组
枚举字串长度h,对于每一次的h,利用height数组,找出连续的height大于等于h的里面最左端和最右端得为之l和r。
如果l+h-1<r的话,说明没有重叠,答案加1.
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std; #define N 1010 int wa[N],wb[N],ws[N],wv[N];
int sa[N],rank1[N],height[N];
int r[N]; char s[N]; int n,ans; int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b] && r[a+l]==r[b+l];
} void da(int *r,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for (i=;i<m;i++) ws[i]=;
for (i=;i<n;i++) ws[x[i]=r[i]]++;
for (i=;i<m;i++) ws[i]+=ws[i-];
for (i=n-;i>=;i--) sa[--ws[x[i]]]=i;
for (j=,p=;p<n;j<<=,m=p)
{
for (p=,i=n-j;i<n;i++) y[p++]=i;
for (i=;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
for (i=;i<n;i++) wv[i]=x[y[i]];
for (i=;i<m;i++) ws[i]=;
for (i=;i<n;i++) ws[wv[i]]++;
for (i=;i<m;i++) ws[i]+=ws[i-];
for (i=n-;i>=;i--) sa[--ws[wv[i]]]=y[i];
for (t=x,x=y,y=t,p=,x[sa[]]=,i=;i<n;i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j) ? p- : p++;
}
return ;
} void calheight(int *r,int n)
{
int i,j,k=;
for (i=;i<=n;i++) rank1[sa[i]]=i;
for (i=;i<n;height[rank1[i++]]=k)
for (k ? k-- : ,j=sa[rank1[i]-];r[i+k]==r[j+k];k++);
return ;
} int main()
{
while (scanf("%s",s)!=EOF && s[]!='#')
{
ans=;
n=strlen(s);
for (int i=;i<n;i++)
r[i]=s[i]-'a'+;
r[n]=;
da(r,n+,);
calheight(r,n);
for (int i=;i<=n/;i++)
{
int l=n+,r=;
for (int j=;j<=n;j++)
{
if (height[j]>=i)
{
r=max(r,sa[j-]);
r=max(r,sa[j]);
l=min(l,sa[j-]);
l=min(l,sa[j]);
}
else
{
if (r-l>=i)
ans++;
r=;
l=n+;
}
}
if (r-l>=i)
ans++;
}
printf("%d\n",ans);
}
return ;
}
【hdu3518】Boring counting的更多相关文章
- 【hdu 3518】Boring counting
		
[链接]h在这里写链接 [题意] 给出一个字符串,求出至少不重叠出现2次以上的子串有多少个. [题解] 枚举要找的子串的长度i; 根据height数组,找出连续>=i的height; 这几个起始 ...
 - 【USACO2017JAN】 Promotion Counting
		
[题目链接] 点击打开链接 [算法] 离散化 + dfs + 树状数组 [代码] #include<bits/stdc++.h> using namespace std; #define ...
 - 【LeetCode】338. Counting Bits (2 solutions)
		
Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
 - 【USACO17JAN】Promotion Counting晋升者计数 线段树+离散化
		
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
 - 【SP26073】DIVCNT1 - Counting Divisors 题解
		
题目描述 定义 \(d(n)\) 为 \(n\) 的正因数的个数,比如 \(d(2) = 2, d(6) = 4\). 令 $ S_1(n) = \sum_{i=1}^n d(i) $ 给定 \(n\ ...
 - 【计数】【UVA11401】 Triangle Counting
		
传送门 Description 把1……n这n个数中任取3个数,求能组成一个三角形的方案个数 Input 多组数据,对于每组数据,包括: 一行一个数i,代表前i个数. 输入结束标识为i<3. O ...
 - 【leetcode】338 .Counting Bits
		
原题 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate t ...
 - 【JZOJ6342】Tiny Counting
		
description analysis 首先不管\(a,b,c,d\)重复的情况方案数是正逆序对之积 如果考虑\(a,b,c,d\)有重复,只有四种情况,下面括号括起来表示该位置重复 比如\(\{a ...
 - 【模板】【P3605】【USACO17JAN】Promotion Counting 晋升者计数——动态开点和线段树合并(树状数组/主席树)
		
(题面来自Luogu) 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1⋯N(1≤N≤100,000) 编号,把公司组织成一棵树 ...
 
随机推荐
- nodejs运行机制
			
有一天老大忽然问起我这个问题,nodejs运行机制 是怎样的?因自己对nodejs也不是很熟悉,就上网查了一下,得出结果如下: 1.简介 Node.js是一个事件驱动I/O服务端JavaScript环 ...
 - <Redis> 入门一 概念安装
			
Redis 概念 redis是一款高性能的NOSQL系列的非关系型数据库 什么是NOSQL NoSQL(NoSQL = Not Only SQL),意即“不仅 ...
 - Android Studio配置Esri ArcGIS
			
1.Android Studio中新建项目: 2.打开project根目录下的build.gradle文件 repositories { jcenter() // Add the following ...
 - Python多线程豆瓣影评API接口爬虫
			
爬虫库 使用简单的requests库,这是一个阻塞的库,速度比较慢. 解析使用XPATH表达式 总体采用类的形式 多线程 使用concurrent.future并发模块,建立线程池,把future对象 ...
 - ZOJ 3905 Cake
			
Cake Time Limit: 4 Seconds Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...
 - 全文搜索(A-2)-推荐算法
			
一般来说推荐算法分为两类. 基于内容过滤的推荐: 基于协同过滤的推荐: 基于内容过滤的推荐,基于特征码描述项目. 协同过滤算法的设计基于一个假设,“和目标用户相似度高的用户,其感兴趣的物品目标用户也会 ...
 - mysql和Oracle  备份表
			
1.SQL Server中,如果目标表存在: insert into 目标表 select * from 原表; 2.SQL Server中,,如果目标表不存在: select * into 目标表 ...
 - 图解Elasticsearch中的_source、_all、store和index属性
			
https://blog.csdn.net/napoay/article/details/62233031
 - hdu - 1394 Minimum Inversion Number(线段树水题)
			
http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...
 - Free Goodies UVA - 12260
			
Petra and Jan have just received a box full of free goodies, and want to divide the goodies between ...