Title: http://poj.org/problem?id=3261
is still a typical application of the suffix array----suffix array +lcp+ two points
That's pretty smooth, 1A.
But most of the time is debugging code. Because the template's global variables are mixed, and you forget it. ,, and so on Xi ' an invitational tournament and four provinces after the end of the calm reflection under the attempt to please template
Error: 1, k with the wrong, the title of K and the template K mixed;
2, or the second part of the C () function, this in fact, in front of a "POJ 1226 Hdu 1238 substrings to find a number of string strings and crossdress the longest common substring 2002 Asian Tianjin Pre-selected topic" C functions almost identical. But it's easier than that, but it's been a while. ,。 At the start. If RET is not recorded, the maximum value of RET should be recorded.
3,last>=kk-1 only, because Lcp[i] itself is two substrings of the public prefix length
int C (int x) { int ret=0,last=0; for (int i=0;i<=n;i++) { if (lcp[i]>=x) ret++; else { Last=max (last,ret); ret=0; } } if (last>=kk-1) return 1; else return 0;}
On the code:
#include <cstdio> #include <iostream> #include <string> #include <algorithm>using namespace std ; const int MAXN = 20200;int rk[maxn],sa[maxn],s[maxn],tmp[maxn],lcp[maxn],n,k,kk;bool cmpsa (int i,int j) {if (Rk[i]! = R K[J]) return rk[i]<rk[j]; else {int ri = i+k<=n?rk[i+k]:-1; int RJ = j+k<=n?rk[j+k]:-1; Return ri<rj; }}void Consa () {for (int i=0;i<=n;i++) sa[i]=i,rk[i]=i<n?s[i]:-1; for (k=1;k<=n;k*=2) {sort (SA,SA+N+1,CMPSA); tmp[sa[0]]=0; for (int i=1;i<=n;i++) {tmp[sa[i]]=tmp[sa[i-1]]+ (CMPSA (Sa[i-1],sa[i])? 1:0); } for (int i=0;i<=n;i++) rk[i]=tmp[i]; }}void CONSTRUCT_LCP () {//n=strlen (s); for (int i=0; i<=n; i++) rk[sa[i]]=i; int h=0; lcp[0]=0; for (int i=0;i<n;i++) {int j=sa[rk[i]-1]; if (h>0) h--; for (; J+h<n && i+h<n; h++) {if (s[j+h]!=s[i+h]) break; } lcp[rk[i]-1]=h; }}int C (int x) {int ret=0,last=0; for (int i=0;i<=n;i++) {if (lcp[i]>=x) ret++; else {Last=max (Last,ret); ret=0; }} if (last>=kk-1) return 1; else return 0;} int main () {//freopen ("Poj 3261.txt", "R", stdin); while (scanf ("%d%d", &n,&kk)!=eof) {for (int i=0;i<n;i++) scanf ("%d", &s[i]); S[n]=-1; Consa (); CONSTRUCT_LCP (); int d=0,up=n+1,mid; while (up>d+1) {mid= (d+up)/2; if (C (mid)) D=mid; else Up=mid; } printf ("%d\n", D); } return 0;}
POJ 3261 suffix array find substrings that appear repeatedly k (substrings can overlap)