LA4513 Stammering Aliens

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 ofs that appears at least m times. For example, in the message baaaababababbababbab, the length-5 word bababis contained 3 times, namely at positions 57 and 12 (where indices start at zero). No substring appearing3 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 example3).

Input 

The input contains several test cases. Each test case consists of a line with an integer  m ( m$ \ge$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 possible starting position of such a substring.

Sample Input 

3
baaaababababbababbab
11
baaaababababbababbab
3
cccccc
0

Sample Output 

5 12
none
4 2
题意:给一个字符串,求该字符串中至少出现m次的最长字符串。
思路:将所给字符串末尾填个最小字符建立后缀数组。然后二分长度确定最长满足出现m次的最长长度。
      然后再去得最大的起始值。先用一个数存目前重叠串的最大起始位置,一旦重叠次数到了m。就更新答案。
      注意下,1要特判下。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
#define maxn 140080
#define inf 0x3f3f3f3f

int str[maxn];
char s[maxn];
int sa[maxn],t[maxn],t2[maxn],c[maxn];
int height[maxn],Rank[maxn];

inline int max(int a,int b)
{
	return a>b?a:b;
}
/*
用SA模板注意在最后添加一个比所有字符都小的字符。
key[n] = 0;
build_sa(key,n+1,m);
getHeight(key,n+1);
显然sa[0] 就是最后那个位置。。。
height[i] 表示 sa[i] 和 sa[i-1] 的最长公共前缀。。
*/

void build_sa(int * s,int n,int m)
{
	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;
		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;
		for(i = 0;i < m;i++)	c[i] = 0;
		for(i = 0;i < n;i++)	c[ x[y[i]] ]++;
		for(i = 0;i < m;i++)	c[i] += c[i-1];
		for(i = n-1;i >= 0;i--)	sa[--c[x[y[i]]]] = y[i];
		//根据sa和y数组计算新的数y组
		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;
	}
}

void getHeight(int * s,int n)
{
	int i,j,k = 0;
	for(i = 0;i < n;i++)	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;
	}
}

bool Judge(int n,int len,int num)
{
	int cnt = 1;
	for(int i = 1;i < n;i++)
	{
		if(height[i] < len)
			cnt = 1;
		else cnt++;
		if(cnt >= num)	return 1;
	}
	return 0;
}

int Get(int n,int len,int num)
{
	int ans = 0;
	int fuck = 0;
	int cnt = 1;
	for(int i = 1;i < n;i++)
	{
		if(height[i] < len)
		{
			fuck = sa[i];
			cnt = 1;
		}
		else cnt++;
		fuck = max(fuck,sa[i]);
		if(cnt >= num)	ans = max(ans,fuck);
	}
	return ans;
}
int main()
{
	//freopen("in.txt","r",stdin);
	int m;
	while(scanf("%d",&m)!=EOF && m)
	{
		scanf("%s",s);
		int len = strlen(s);
		if(m == 1)
		{
			printf("%d %d\n",len,0);
			continue;
		}
		for(int i = 0;i < len;i++)
		{
			str[i] = s[i] - 'a' + 1;
		}
		str[len] = 0;
		build_sa(str,len+1,28);
		getHeight(str,len+1);
		int l = 0,r = len;
		int maxlen = 0;
		while(l <= r)
		{
			int mid = (l+r) >> 1;
			if(Judge(len+1,mid,m))
			{
				l = mid + 1;
				maxlen = mid;
			}
			else r = mid - 1;
		}
		if(maxlen == 0)	printf("none\n");
		else	printf("%d %d\n",maxlen,Get(len+1,maxlen,m));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值