题目

Source

http://acm.hdu.edu.cn/showproblem.php?pid=4080

Description

Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all efforts to decode their messages have failed so far because, as luck would have it, they have stumbled upon a race of stuttering aliens! Her team has found out that, in every long enough message, the most important words appear repeated a certain number of times as a sequence of consecutive characters, even in the middle of other words. Furthermore, sometimes they use contractions in an obscure manner. For example, if they need to say bab twice, they might just send the message babab, which has been abbreviated because the second b of the first word can be reused as the first b of the second one.
Thus, the message contains possibly overlapping repetitions of the same words over and over again. As a result, Ellie turns to you, S.R. Hadden, for help in identifying the gist of the message.
Given an integer m, and a string s, representing the message, your task is to find the longest substring of s that appears at least m times. For example, in the message baaaababababbababbab, the length-5 word babab is contained 3 times, namely at positions 5, 7 and 12 (where indices start at zero). No substring appearing 3 or more times is longer (see the first example from the sample input). On the other hand, no substring appears 11 times or more (see example 2). In case there are several solutions, the substring with the rightmost occurrence is preferred (see example 3).

Input

The input contains several test cases. Each test case consists of a line with an integer m (m >= 1), the minimum number of repetitions, followed by a line containing a string s of length between m and 40 000, inclusive. All characters in s are lowercase characters from "a" to "z". The last test case is denoted by m = 0 and must not be processed.

Output

Print one line of output for each test case. If there is no solution, output none; otherwise, print two integers in a line, separated by a space. The first integer denotes the maximum length of a substring appearing at least m times; the second integer gives the rightmost starting position of this substring.

Sample Input

3
baaaababababbababbab
11
baaaababababbababbab
3
cccccc
0

Sample Output

5 12
none
4 2

分析

题目大概说给一个字符串,求出现至少m的子串的最长长度以及出现在最右边的位置。

经典的后缀数组题目吧,二分长度,height分组判定长度是否成立。。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 44444int wa[MAXN],wb[MAXN],wv[MAXN],ws[MAXN];
int cmp(int *r,int a,int b,int l){return r[a]==r[b] && r[a+l]==r[b+l];
}
int sa[MAXN],rnk[MAXN],height[MAXN];
void SA(int *r,int n,int m){int *x=wa,*y=wb;for(int i=0; i<m; ++i) ws[i]=0;for(int i=0; i<n; ++i) ++ws[x[i]=r[i]];for(int i=1; i<m; ++i) ws[i]+=ws[i-1];for(int i=n-1; i>=0; --i) sa[--ws[x[i]]]=i;int p=1;for(int j=1; p<n; j<<=1,m=p){p=0;for(int i=n-j; i<n; ++i) y[p++]=i;for(int i=0; i<n; ++i) if(sa[i]>=j) y[p++]=sa[i]-j;for(int i=0; i<n; ++i) wv[i]=x[y[i]];for(int i=0; i<m; ++i) ws[i]=0;for(int i=0; i<n; ++i) ++ws[wv[i]];for(int i=1; i<m; ++i) ws[i]+=ws[i-1];for(int i=n-1; i>=0; --i) sa[--ws[wv[i]]]=y[i];swap(x,y); x[sa[0]]=0; p=1;for(int i=1; i<n; ++i) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;}for(int i=1; i<n; ++i) rnk[sa[i]]=i;int k=0;for(int i=0; i<n-1; height[rnk[i++]]=k){if(k) --k;for(int j=sa[rnk[i]-1]; r[i+k]==r[j+k]; ++k);}
}char str[MAXN];
int n,m,a[MAXN];bool isOK(int len){int left=-1,right,mx=0;for(int i=2; i<=n; ++i){if(height[i]>=len){if(left==-1) left=i-1;right=i;}else{left=-1;}if(left!=-1) mx=max(mx,right-left+1);}return mx>=m;
}int getRightMost(int len){int left=-1,right,mx=0,tmp=0;for(int i=2; i<=n; ++i){if(height[i]>=len){if(left==-1) left=i-1;right=i;tmp=max(tmp,max(sa[i-1],sa[i]));}else{left=-1;tmp=0;}if(left!=-1){if(right-left+1>=m) mx=max(mx,tmp);}}return mx;
}int main(){while(scanf("%d",&m)==1 && m){scanf("%s",str);n=strlen(str);if(m==1){printf("%d %d\n",n,0);continue;}for(int i=0; i<n; ++i){a[i]=str[i]-'a'+1;}a[n]=0;SA(a,n+1,28);int l=0,r=MAXN;while(l<r){int mid=l+r+1>>1;if(isOK(mid)) l=mid;else r=mid-1;}if(l==0){puts("none");continue;}printf("%d %d\n",l,getRightMost(l));}return 0;
}

转载于:https://www.cnblogs.com/WABoss/p/5830269.html

HDU4080 Stammering Aliens(二分 + 后缀数组)相关推荐

  1. POJ - 3450 Corporate Identity(二分+后缀数组)

    题目链接:点击查看 题目大意:给出n个字符串,求出n个字符串中最长的公共子串,如果没有,输出IDENTITY LOST 题目分析:可以直接用二分+后缀数组来做,先将n个字符串连接起来,中间用一个不同的 ...

  2. POJ1226 Substrings(二分+后缀数组)

    题意:给n个字符串,求最长的子串,满足它或它的逆置出现在所有的n个字符串中. 把n个字符串及其它们的逆置拼接,中间用不同字符隔开,并记录suffix(i)是属于哪个字符串的: 跑后缀数组计算heigh ...

  3. POJ - 3261 Milk Patterns(二分+后缀数组)

    题目链接:点击查看 题目大意:给出一个字符串,以及一个k,现在求出现次数大于等于k次的最大可重叠子串的长度 题目分析:可以说是后缀数组的模板题目了吧..直接跑出height数组,因为height数组代 ...

  4. POJ - 1743 Musical Theme(二分+后缀数组+差分数组)

    题目链接:点击查看 题目大意:给出n个连续的数字组成的序列,现在要求出其中两个不重叠的字序列,满足两个子序列"相似",相似的定义是两个子序列当且仅当长度相等并且每一位的数字差都相等 ...

  5. HDU5853 Jong Hyok and String(二分 + 后缀数组)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5853 Description Jong Hyok loves strings. One da ...

  6. POJ - 3294 Life Forms(二分+后缀数组)

    题目链接:点击查看 题目大意:给出n个字符串,求出至少在n/2个字符串中出现的最长的公共子串,如果没有,输出"?" 题目分析:和poj3450几乎一样的题目,只不过最长公共子串出现 ...

  7. hdu-4080 Stammering Aliens 字符串hash 模板题

    http://acm.hdu.edu.cn/showproblem.php?pid=4080 求出现次数大于等于n的最长串. #include<iostream> #include< ...

  8. Musical Theme pku1743 (后缀数组)

    Musical Theme(后缀数组) 题意: n个数,选取一段子序列,满足以下条件: 1.长度至少为5 2.在数列中其他位置出现过(允许转置) 3.与其他位置出现的不重叠 转置:将恒定的正或负值添加 ...

  9. Hash(LCP) || 后缀数组 LA 4513 Stammering Aliens

    题目传送门 题意:训练指南P225 分析:二分寻找长度,用hash值来比较长度为L的字串是否相等. #include <bits/stdc++.h> using namespace std ...

最新文章

  1. 15.concurrent-control并发控制
  2. 多边形对角线条数(C语言)
  3. Asia Yokohama Regional Contest 2018 G题 What Goes Up Must Come Down(树状数组求逆序对)
  4. android 注解点击事件,android click事件注解
  5. 南京林业大学转计算机专业好转吗,南京林业大学如何转专业
  6. 桥接模式Bridge
  7. 去掉Mac窗口截图自带的阴影?
  8. alisql mysql_AliSQL · 特性介绍 · 动态加字段
  9. Python机器学习笔记 GridSearchCV
  10. 手把手教你在树莓派上搭建ghost个人博客呦
  11. R语言-聚类分析(系统聚类)
  12. Google测试之道读后感
  13. 咖说 | 隐私何在?区块链是隐私保护的安全阀门
  14. 【NOI2015】小园丁与老司机
  15. 7月上热搜50次!周杰伦新专辑1天1.5亿!歌手新歌爆红的营销路径
  16. 慢阻肺患者安全过冬指南
  17. 电脑无线(外网)和有线(内网)网络同时使用方法
  18. 备案域名服务器DNS修改,未备案域名也可以用高防CDN加速
  19. 华为手机忘记密码如何解开,有什么相关教程吗
  20. str开头的c语言函数介绍,C语言str函数系列

热门文章

  1. 数字货币EOS半年时间暴跌90%多,还可追捧吗?
  2. 软件测试质量过程检测文档_如何编写实际上有效的质量检查文档
  3. php 保存表单数据,使用jquery和php自动保存表单数据
  4. (C++)1046 划拳
  5. hung-yi lee_p12_深度学习简介
  6. leetcode--对称二叉树--python
  7. 使用wsdl2java命令生成webservice本地调用代码
  8. 90 后 CTO 创业 6 年,做了一件改变互联网的“小事”
  9. 七牛云内容审核服务被选为「上海首批人工智能创新产品」
  10. 定期删除各子文件下数据