POJ 3261 overlapping K-times longest repeating substring "suffix array"

Source: Internet
Author: User

This is an example.

Given a string, the longest repeating substring that appears at least k times, which can overlap.
Algorithm Analysis:
This problem is similar to the previous question, is the first two answers, and then divided into groups of suffixes. No
The same is, here to determine whether there is a group suffix number is not less than K. If there is, then there is
K the same substring satisfies a condition, otherwise it does not exist. The time complexity of this procedure is O (NLOGN).

Source Code:

//#pragma COMMENT (linker, "/stack:16777216")//For C + + Compiler#include <stdio.h>#include<iostream>#include<fstream>#include<cstring>#include<cmath>#include<stack>#include<string>#include<map>#include<Set>#include<list>#include<queue>#include<vector>#include<algorithm>#defineMax (b) ((a) > (b))? (a): (b))#defineMin (b) ((a) < (a))? (a): (b))#defineAbs (x) (((x) > 0)? (x): (-(x)))#defineMOD 1000000007#definePi ACOs (-1.0)using namespaceStd;typedefLong Longll; typedef unsignedLong Longull; typedef unsignedint        UINT; typedef unsignedCharUchar; template<classT> InlinevoidCheckmin (T &a,t b) {if(a>b) a=b;} Template<classT> InlinevoidCheckmax (T &a,t b) {if(a<b) a=b;}Const DoubleEPS = 1e-7      ;Const intN =1              ;Const intM =200000         ;Constll P =10000000097ll;Const intINF =0x3f3f3f3f   ;intA[m], sa[m], h[m], rank[m];voidRadixint*STR,int*a,int*b,intNintm) {    Static intCount[m]; inti; memset (Count,0,sizeof(count));  for(i =0; I < n; ++i) + +Count[str[a[i]];  for(i =1; I <= m; ++i) Count[i] + = count[i-1];  for(i = n-1; I >=0; -i) b[--count[str[a[i] []] =a[i];}voidSuffix_array (int*STR,int*sa,intNintm) {    Static intA[m], b[m]; intI, J;  for(i =0; I < n; ++i) Rank[i] =i;    Radix (str, rank, SA, N, M); rank[sa[0]] =0;  for(i =1; I < n; ++i) Rank[sa[i] = rank[sa[i-1]] + (str[sa[i]]! = str[sa[i-1]]);  for(i =0;1<<i < n; ++i) {         for(j =0; J < N; ++j) {A[j]= Rank[j] +1; B[J]= j + (1<< i) >= n?0: Rank[j + (1<< i)] +1; SA[J]=J;        } radix (b, SA, rank, n, N);        Radix (A, rank, SA, n, N); rank[sa[0]] =0;  for(j =1; J < N; ++j) {Rank[sa[j]]= Rank[sa[j-1]] + (A[SA[J-1]]! = A[sa[j] | | B[sa[j-1]] !=B[sa[j]]); }    }}voidCalc_height (int*STR,int*sa,int*h,intN) {    intI, k =0; h[0] =0;  for(i =0; I < n; ++i) {k= k = =0?0K1; if(Rank[i]! =0){             while(Str[i + K] = = Str[sa[rank[i]-1] +K]) {                ++K; }} H[rank[i]]=K; }}voidSOLVE_DUPLICATE_SUBSTR (intN) {    intI, J, pos, ans =0;  for(i =0; I < n; ++i) {        if(H[rank[i]) >ans) {ans=H[rank[i]]; POS=i; }    }     for(i = pos; I < pos + ans; + +)i) {printf ("%c", A[i]); } printf ("\ n");}voidSLOVE_UPDATE_DUPLICATE_SUBSTR (intNintk) {    intI, J; intLow =1, high =N; intAns =0, POS1 =0, Pos2 =0;  while(Low <=High ) {        intMid = (low + high)/2; BOOLFlag =false;  for(i =0; I < n; ++i) {            if(H[i] >=mid) {                ++ans; if(ans >= k) flag =true; }            ElseAns =1; }        if(flag) Low = mid +1; ElseHigh = mid-1; } cout<< High <<Endl;}intMain () {intI, J, T, N, M, K;  while(Cin >> N >>k) {         for(i =0; I < n; ++i) Cin >>A[i]; Suffix_array (A, SA, N, the);        Calc_height (A, SA, H, N);    Slove_update_duplicate_substr (n, k); }    return 0;}

POJ 3261 overlapping K-times longest repeating substring "suffix array"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.