HUST 1352 Repetitions of Substrings(字符串)
Description
Input
Output
Sample Input
abcabc 2
acmac 3
Sample Output
1
0 先记录一下,完全不知道怎么回事。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int maxn=;
int height[maxn],sa[maxn],rank[maxn],c[maxn],t1[maxn],t2[maxn];
void da(int *str,int n,int m)
{
int i,j,k,p,*x=t1,*y=t2;
for(i=;i<m;i++)c[i]=;
for(i=;i<n;i++)c[x[i]=str[i]]++;
for(i=;i<m;i++)c[i]+=c[i-];
for(i=n-;i>=;i--)sa[--c[x[i]]]=i;
for(k=;k<=n;k<<=)
{
p=;
for(i=n-k;i<n;i++)y[p++]=i;
for(i=;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
for(i=;i<m;i++)c[i]=;
for(i=;i<n;i++)c[x[y[i]]]++;
for(i=;i<m;i++)c[i]+=c[i-];
for(i=n-;i>=;i--)sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=;x[sa[]]=;
for(i=;i<n;i++)
x[sa[i]]=y[sa[i]]==y[sa[i-]]&&y[sa[i]+k]==y[sa[i-]+k]?p-:p++;
if(p>=n)break;
m=p;
}
}
void calheight(int *str,int n)
{
int i,j,k=;
for(i=;i<=n;i++)rank[sa[i]]=i;
for(i=;i<n;i++)
{
if(k)k--;
j=sa[rank[i]-];
while(str[i+k]==str[j+k])k++;
height[rank[i]]=k;
}
// printf("sa:");for(i=0;i<=n;i++)printf("%d ",sa[i]);puts("");
// printf("rank:");for(i=0;i<=n;i++)printf("%d ",rank[i]);puts("");
// printf("height:");for(i=0;i<=n;i++)printf("%d ",height[i]);puts(""); }
int str[maxn];
char ss[maxn];
int Log[maxn];
int best[][maxn];
void init(int n)
{
int i,j;
Log[]=-;
for(i=;i<=n;i++)
Log[i]=(i&(i-))?Log[i-]:Log[i-]+;
for(i=;i<=n;i++)best[][i]=height[i];
for(i=;i<=Log[n];i++)
for(j=;j<=n;j++)
best[i][j]=min(best[i-][j],best[i-][j+(<<(i-))]);
}
int lcp(int a,int b)
{
a=rank[a];
b=rank[b];
if(a>b)swap(a,b);
a++;
int t=Log[b-a+];
return min(best[t][a],best[t][b-(<<t)+]);
}
int rep[maxn];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int i,j,k,m,n,p;
while(~scanf("%s%d",&ss,&k))
{
n=strlen(ss);
if(k==)
{
printf("%lld\n",(long long)n*(n+)/);
continue;
}
for(i=;i<n;i++)str[i]=ss[i];
str[n]=;
da(str,n+,);
calheight(str,n);
init(n);
for(i=;i<=n;i++)rep[i]=;
for(int L=;L*k<=n;L++)
{
for(i=;i<n;i+=L)
{
int t=lcp(i,i+L);
if(!t)continue;
j=;
while(j<=i&&j<L&&str[i-j]==str[i-j+L])
{
if(t>=L&&lcp(i-j,i-j+L)>=(k-)*L)
rep[i-j]=max(rep[i-j],t/L+);
j++;t++;
}
}
}
int ans=;
for(i=;i<=n;i++)if(rep[i]>=k)ans+=rep[i]-k+;
printf("%d\n",ans);
}
return ;
}
HUST 1352 Repetitions of Substrings(字符串)的更多相关文章
- Codeforces 316G3 Good Substrings 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/9010851.html 题目传送门 - Codeforces 316G3 题意 给定一个母串$s$,问母串$s$有 ...
- Codeforces Round #294 (Div. 2)D - A and B and Interesting Substrings 字符串
D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 megaby ...
- HUST 1010 The Minimum Length (字符串最小循环节)
题意 有一个字符串A,一次次的重写A,会得到一个新的字符串AAAAAAAA.....,现在将这个字符串从中切去一部分得到一个字符串B.例如有一个字符串A="abcdefg".,复制 ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- FFT题集
FFT学习参考这两篇博客,很详细,结合这看,互补. 博客一 博客二 很大一部分题目需要构造多项式相乘来进行计数问题. 1. HDU 1402 A * B Problem Plus 把A和B分别当作多项 ...
- 在 C# 中使用 Span<T> 和 Memory<T> 编写高性能代码
目录 在 C# 中使用 Span 和 Memory 编写高性能代码 .NET 中支持的内存类型 .NET Core 2.1 中新增的类型 访问连续内存: Span 和 Memory Span 介绍 C ...
- LA4671 K-neighbor substrings(FFT + 字符串Hash)
题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
随机推荐
- ascii 转换为 utf-8
Python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错: UnicodeDecodeError: 'ascii' codec can't de ...
- Genymotion下载失败解决方法
Genymotion下载虚拟机版本时会很慢,而且经常下载失败 解决方法如下: 1.先去选择下载你需要的版本,之后会下载(很慢),或者失败. 2.到C:\Users\yourname\AppData\L ...
- JS 获取网页的宽高
网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...
- Beego学习笔记——开始
beego简介 beego是一个快速开发Go应用的http框架,他可以用来快速开发API.Web.后端服务等各种应用,是一个RESTFul的框架,主要设计灵感来源于tornado.sinatra.fl ...
- 7.MyBatis延时加载
1.创建javaWeb项目MyBatis_Lazy并在WebRoot下的WEB-INF下的lib下添加如下jar文件 cglib-nodep-2.1_3.jar log4j-1.2.17.jar my ...
- c#语言简介
语言的历史:c,c++,java,c#, c语言最早,c++语言复杂,而java把其中难的一部分去掉,也成为c--,而c#早期类似java. c#简单高效,并且与web结合. C#的运行环境是.net ...
- Windows 多用户远程访问 Ubuntu 14.04桌面
使用X2Go实现多用户远程访问 Ubuntu 14.04桌面:VNC也可以,但是每次连接VNC就回新创建一个Seession,想要在下次远程登录的时候返回上次活动,需要记住开启的线程,这种繁琐的操作不 ...
- LeetCode 385. Mini Parse
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- UNIX基础--Shells
Shells Shell提供了一个和操作系统交互的命令行接口.shell的主要功能就是从输入取得命令然后去执行.FreeBSD内含了一些shell,包括:Bourne shell(sh). exten ...
- ansible role 执行顺序
the dependencies of the 'openshift-master' role.- docker- openshif_common - os_firewall - openshi ...