SETI

Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

Problem Description

Amateur astronomers Tom and Bob try to find radio broadcasts of extraterrestrial civilizations in the air. Recently they received some strange signal and represented it as a word consisting of small letters of the English alphabet. Now they wish to decode the signal. But they do not know what to start with.
      They think that the extraterrestrial message consists of words, but they cannot identify them. Tom and Bob call a subword of the message a potential word if it has at least two non-overlapping occurrences in the message.

For example, if the message is “abacabacaba”, “abac” is a potential word, but “acaba” is not because two of its occurrences overlap.
      Given a message m help Tom and Bob to find the number of potential words in it.

Input

      Input file contains one string that consists of small letters of the English alphabet. The length of the message doesn’t exceed 10 000.

Output

      Output one integer number — the number of potential words in a message.

Sample Input

abacabacaba

Sample Output

15

Source

Andrew Stankevich Contest 23
题目大意:给你一个串,让求该串的不重叠重复子串个数有多少个。
 
解题思路:后缀数组模板题。构造该字符串的height数组,然后枚举公共前缀长度为1---n/2的子串有多少个,累加结果。
 
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int maxn = 1e5+200;
const int INF = 0x3f3f3f3f;
char s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn];
int rank[maxn], height[maxn];
void build_sa(int n, int m){ //构造sa数组
int i,*x = t, *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(int k = 1; k <= n; k <<= 1){
int p = 0;
//直接利用上次的sa数组排序第二关键字,得到本次的伪sa数组
for(i = n-k; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i]-k; //y是伪sa数组
//基数排序第一关键字,利用本次伪sa数组和上次的排名数组得到本次的sa数组
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[y[i]]]++; //这里的x数组类似于后来要求的rank数组
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];
//交换后,y数组变为上次的排名数组,然后根据本次sa和上次的排名数组y得到本次的排名数组x
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]+k] ==y[sa[i]+k] ? p-1:p++;
if(p >= n) break;
m = p;
}
return ;
}
void getheight(int n) { int i, j, k = 0;
for(i = 0; i < n; i++) { //从i = 0 到 i = n-1是有用的
rank[sa[i]] = i;
}
for(i = 0; i < n; i++) {
if(k) k--;
int j = sa[rank[i]-1];
while(s[i+k] == s[j+k]){
k++;
}
height[rank[i]] = k;
}
}
int check(int mid , int n){
int mi=INF , mx = 0, num=0, ret = 0;
for(int i = 2;i <= n+1;i++){ //i = 2 因为最小的肯定是尾字符,所以i从2开始计算
if(i==n+1 || height[i] < mid){
// printf("%d %d %d\n",i,height[i],mid);
mi = min(mi, sa[i-1]);
mx = max(mx, sa[i-1]);
if(mx - mi >= mid &&num >= 1){ //mx - mi >= mid表示不重叠 num >= 1表示存在重复子串
ret ++;
}
mx = 0; mi = INF;
num = 0;
}
else if(height[i] >= mid){ //表示sa[i]跟sa[i-1]的公共前缀长度大于mid
mi = min(mi,sa[i-1]);
mx = max(mx,sa[i-1]);
num++;
}
}
return ret;
}
int main(){
int n;
while(scanf("%s",s)!=EOF){
n = strlen(s);
// s[n++] = '#';
// s[n] = '\0';
build_sa(n+1,200); //如果想让字符串结尾符'\0'作为尾字符,那么就传参n+1
build_sa(n, 200); //或者加一个'#'作为尾字符,传参n。 尾字符一定要是所有字符中最小的,且大于等于0 getheight(n+1); //跟构造sa同理 int ans = 0 ,res;
for(int i = 1; i <= n/2; i++){
if(res = check(i , n)){ //枚举各个公共前缀长度
// printf("%d %d+++\n", i, res);
ans += res;
}
}
printf("%d\n",ans);
}
return 0;
}

  

ACdream 1430——SETI——————【后缀数组,不重叠重复子串个数】的更多相关文章

  1. [ACDream 1430]SETI 后缀数组

    题目链接:http://acdream.info/problem?pid=1430 题目大意:给你一个长度不超过10000的字符串,问你出现过两次或两次以上的不重叠的子串有多少个. 后缀数组计算出he ...

  2. acdream 1430 SETI 后缀数组+height分组

    这题昨天比赛的时候逗了,后缀想不出来,由于n^2的T了,就没往后缀数组想--并且之后解题的人又说用二分套二分来做.然后就更不会了-- 刚才看了题解,唉--原来题讲解n^2的也能够过,然后就--这样了! ...

  3. 【SPOJ – REPEATS】 后缀数组【连续重复子串】

    字体颜色如何 字体颜色 SPOJ - REPEATS 题意 给出一个字符串,求重复次数最多的连续重复子串. 题解 引自论文-后缀数组--处理字符串的有力工具. 解释参考博客 "S肯定包括了字 ...

  4. SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数

    题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...

  5. POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数

    题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K ...

  6. POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)

    永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...

  7. poj 1743 后缀数组 求最长不重叠重复子串

    题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. “主题”是整个音符序列的一个子串,它需要满足如下条件:1 ...

  8. poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)

    题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...

  9. SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转

    694. Distinct Substrings Problem code: DISUBSTR   Given a string, we need to find the total number o ...

随机推荐

  1. windows修改远程桌面RDP连接数

    windows 2003在默认情况下最多只允许两个用户进行远程终端连接,当达到两个远程桌面连接的到时候,再有人尝试连接,就会提示已经达到最大终端数,无法连上了. 一.windows2003终端连接数修 ...

  2. shell入门-awk-1

    awk对于sed grep的优势在于分段,把分隔符分割成一小段一小段,针对段进行匹配,替换,操作,逻辑判断,比较...等 awk的基本操作 [root@wangshaojun ~]# awk -F ' ...

  3. javaScript之事件处理程序

    事件就是用户或浏览器自身执行的某个动作,JavaScript与HTML的交互也是通过事件实现的.而相应某个事件的函数就叫做事件处理函数.包括以下几种: 1.HTML事件处理程序    某个元素支持的每 ...

  4. LDA与最小二乘法的关系及其变种详解

    1 LDA与最小二乘法的关联 对于二值分类问题,令人惊奇的是最小二乘法和LDA分析是一致的.回顾之前的线性回归,给定N个d维特征的训练样例(i从1到N),每个对应一个类标签.我们之前令y=0表示一类, ...

  5. TCP的三次握手和四次挥手,为什么?

    首先,我们要知道TCP是全双工的,即客户端在给服务器端发送信息的同时,服务器端也可以给客户端发送信息.而半双工的意思是A可以给B发,B也可以给A发,但是A在给B发的时候,B不能给A发,即不同时,为半双 ...

  6. 获取剪切板上DataFormats.Dib格式的文件

    if (formats.Contains(System.Windows.Forms.DataFormats.Dib)) { using (var img = System.Windows.Forms. ...

  7. 文件格式——fastq格式

    fastQ格式 FASTQ是一种存储了生物序列(通常是核酸序列)以及相应的质量评价的文本格式. 他们都是以ASCII编码的.现在几乎是高通量测序的标准格式.NCBI Short Read Archiv ...

  8. 《精通Spring4.X企业应用开发实战》读后感第六章(属性编辑器)

  9. Jenkins配置有用摘抄笔记

    使用jenkins配置.net mvc5网站自动构建全过程记录  转自:http://www.cnblogs.com/baiyunchen/p/4724350.html 持续集成是个简单重复劳动,人来 ...

  10. 【Qt文档阅读】事件系统

    在Qt中,事件对象都继承于QEvent类,它表示应用程序内部或由于应用程序需要了解的外部活动而发生的事情.事件可以由QObject子类的任何实例接收和处理,尤其是widget.本文档描述如何在典型应用 ...