HDU4080Stammering Aliens(后缀数组+二分)
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).
InputThe 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.
OutputPrint 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次的最长子序列(可交叉),没有输出none,有则输出长度,和最后一次出现的位置。
思路:
求出后缀数组,二分长度,得到长度,至于最后一次的位置,需要把每个位置枚举一下,即大于Mid的ht[i],对应着的sa[i],和sa[i-1]。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
const int maxn=;
char ch[maxn]; int m;
int max(int x,int y){if(x>y) return x;return y;}
struct SA
{
int Rank[maxn],sa[maxn],tsa[maxn],cntA[maxn],cntB[maxn],A[maxn],B[maxn];
int ht[maxn],N;
void get_sa()
{
N=strlen(ch+);
for(int i=;i<=;i++) cntA[i]=;
for(int i=;i<=N;i++) cntA[ch[i]]++;
for(int i=;i<=;i++) cntA[i]+=cntA[i-];
for(int i=N;i>=;i--) sa[cntA[ch[i]]--]=i;
Rank[sa[]]=;
for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(ch[sa[i]]==ch[sa[i-]]?:);
for(int l=;Rank[sa[N]]<N;l<<=){
for(int i=;i<=N;i++) cntA[i]=cntB[i]=;
for(int i=;i<=N;i++) cntA[A[i]=Rank[i]]++;
for(int i=;i<=N;i++) cntB[B[i]=i+l<=N?Rank[i+l]:]++;
for(int i=;i<=N;i++) cntA[i]+=cntA[i-],cntB[i]+=cntB[i-];
for(int i=N;i>=;i--) tsa[cntB[B[i]]--]=i;
for(int i=N;i>=;i--) sa[cntA[A[tsa[i]]]--]=tsa[i];
Rank[sa[]]=;
for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(A[sa[i]]==A[sa[i-]]&&B[sa[i]]==B[sa[i-]]?:);
}
}
void get_ht()
{
for(int i=,j=;i<=N;i++){
if(j) j--;
while(ch[i+j]==ch[sa[Rank[i]-]+j]) j++;
ht[Rank[i]]=j;
}
}
int check(int L)
{
int cnt=,tmppos=,pos=;
for(int i=;i<=N;i++){
if(ht[i]<L) cnt=,tmppos=;
else cnt++,tmppos=max(tmppos,max(sa[i],sa[i-]));
if(cnt>=m) pos=max(tmppos,pos);
} return pos;
}
void solve()
{
//for(int i=1;i<=N;i++) printf("%d ",sa[i]);printf("\n");
//for(int i=1;i<=N;i++) printf("%d ",ht[i]);printf("\n");
int L=,R=N,ans=,tmp,anspos=;
while(L<=R){
int Mid=(L+R)>>;
tmp=check(Mid);
if(tmp) {
ans=Mid;anspos=tmp; L=Mid+;
} else R=Mid-;
}
if(ans==) printf("none\n");
else printf("%d %d\n",ans,anspos-);
}
}Sa;
int main()
{
int i,j,k;
while(~scanf("%d",&m)&&m){
scanf("%s",ch+);
if(m==){
printf("%d 0\n",strlen(ch+));
continue;
}
Sa.get_sa();
Sa.get_ht();
Sa.solve();
} return ;
}
HDU4080Stammering Aliens(后缀数组+二分)的更多相关文章
- BZOJ 3230: 相似子串( RMQ + 后缀数组 + 二分 )
二分查找求出k大串, 然后正反做后缀数组, RMQ求LCP, 时间复杂度O(NlogN+logN) -------------------------------------------------- ...
- BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案
BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l 读入单 ...
- 【bzoj4310】跳蚤 后缀数组+二分
题目描述 很久很久以前,森林里住着一群跳蚤.一天,跳蚤国王得到了一个神秘的字符串,它想进行研究. 首先,他会把串分成不超过 k 个子串,然后对于每个子串 S,他会从S的所有子串中选择字典序最大的那一个 ...
- BZOJ 1717 [USACO06DEC] Milk Patterns (后缀数组+二分)
题目大意:求可重叠的相同子串数量至少是K的子串最长长度 洛谷传送门 依然是后缀数组+二分,先用后缀数组处理出height 每次二分出一个长度x,然后去验证,在排序的后缀串集合里,有没有连续数量多于K个 ...
- POJ 1743 [USACO5.1] Musical Theme (后缀数组+二分)
洛谷P2743传送门 题目大意:给你一个序列,求其中最长的一对相似等长子串 一对合法的相似子串被定义为: 1.任意一个子串长度都大于等于5 2.不能有重叠部分 3.其中一个子串可以在全部+/-某个值后 ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- Poj 3261 Milk Patterns(后缀数组+二分答案)
Milk Patterns Case Time Limit: 2000MS Description Farmer John has noticed that the quality of milk g ...
- Uva12206 Stammering Aliens 后缀数组&&Hash
Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all effort ...
- UVA 12206 - Stammering Aliens(后缀数组)
UVA 12206 - Stammering Aliens 题目链接 题意:给定一个序列,求出出现次数大于m,长度最长的子串的最大下标 思路:后缀数组.搞出height数组后,利用二分去查找就可以 这 ...
随机推荐
- zoj 2949 - Coins of Luck
题目:有2中面条各n碗.每次抛硬币推断吃哪一种(到一种吃完为止).问抛硬币的数学期望. 分析:动态规划.概率dp.求出每种结束状态(即,有一种吃完)的概率,分别乘以步长即为期望. 大黄解法:状态位剩余 ...
- 团队项目的Git分支管理规范
原文地址: http://blog.jboost.cn/2019/06/17/git-branch.html 许多公司的开发团队都采用Git来做代码版本控制.如何有效地协同开发人员之间,以及开发.测试 ...
- c# .net 关于接口实现方式:隐式实现/显式实现!
以前在用到接口时,从来没注意到接口分为隐式实现与显示实现.昨天在浏览博客时看到相关内容,现在根据自己的理解记录一下,方便日后碰到的时候温习温习. 通俗的来讲,“显示接口实现”就是使用接口名称作为方法 ...
- PowerBuilder -- 调试(Debug)
@.进入代码调试,执行下一步直接就退出了调试 原文:http://bbs.csdn.net/topics/390126005 处理方法:尝试把 Watch 中查看的变量全部删掉.
- 开源项目Universal Image Loader for Android 说明文档 (1) 简单介绍
When developing applications for Android, one often facesthe problem of displaying some graphical ...
- 【BZOJ】1003 Cards
[解析]Burnside引理+背包dp+乘法逆元 [Analysis] 这道题卡了好久,就是没想懂置换跟着色是不一样的. 依据burnside引理.在一个置换群作用下不等价类的个数为每一个置换作用下不 ...
- HTML5,不仅仅是看上去非常美(第二弹:打造最美3D机房)
前言 近期项目开发任务告一段落,刚好有时间整理这大半年的一些成果.使用html5时间还不久,对js的认识还不够深入.没办法,曾经一直搞java,对js的一些语言特性和概念一时还转换只是来. 上一篇第一 ...
- Angular入门(一) 环境配置
angular/cli 安装 ♦ npm uninstall -g angular-cli /cnpm install -g angular-cli ※采用npm安装失败: Missing write ...
- C#Panel 控件的使用
Windows 窗体 Panel 控件用于为其他控件提供可识别的分组.通常,使用面板按功能细分窗体.例如,可能有一个订单窗体,它指定邮寄选项(如使用哪一类通营承运商).将所有选项分组在一个面板中可向用 ...
- 关于String,StringBuffer与StringBuilder的区别
String是字符串常量对象,对其进行改变时会相当影响效率,特别注意在循环中直接拼接字符串效率非常差. 如果你想改变字符串的值,更加推荐使用StringBuffer与StringBuilder两种可变 ...