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 5, 7 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
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; }