Hdu 6194 Shenyang Network Competition -- string (suffix array), hdu -- string

Source: Internet
Author: User

Hdu 6194 Shenyang Network Competition -- string (suffix array), hdu -- string

Question Link

 

Problem DescriptionUncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle Mao was so lazy that he left the problem to you. I hope you can give him a solution.
Given a string s, we define a substring that happens exactly k times as an important string, and you need to find out how many substrings which are important strings.
 

 

InputThe first line contains an integer T (T ≤ 100) implying the number of test cases.
For each test case, there are two lines:
The first line contains an integer k (k ≥ 1) which is described above;
The second line contain a string s (length (s) ≤ 105 ).
It's guaranteed that Σ length (s) ≤ 2 bytes 106.

 

OutputFor each test case, print the number of the important substrings in a line.

 

Sample Input22abcabc3abcabcabcabc

 

Sample Output69. There is a string s. How many substrings exactly appear k times? Idea: suffix array. You can use the suffix array algorithm to know the ranking of each suffix. If a substring occurs exactly k times, then there must be k corresponding suffixes, that is, this substring is the prefix of these k suffix strings, so the ranking of these k suffix strings must be continuous, so we rank from 1 ~ Len (s) starts to fetch k consecutive suffix strings. You can quickly calculate the maximum common prefix length len of the k suffix strings Based on the height [] array, so long as 1 to len prefix substrings, these k strings contain, set the current start k strings to I + k-1, then if the substring length is too short, possible I-1 or I + k this string also contains the corresponding substring, So calculate the I and I-1 string, I + k and I + k-1 maximum common prefix is m, so the length of the substring must be greater than m to ensure that the I-1 and I + k do not contain the corresponding substring, only I ~ The I + k-1 k strings contain corresponding substrings. The Code is as follows:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;typedef long long LL;const int N=1e5+5;char s[N];int k;int wa[N],wb[N],wv[N],wss[N];int sa[N],ran[N],height[N];int f[N][20];int cmp(int *r,int a,int b,int l){    return r[a]==r[b]&&r[a+l]==r[b+l];}void da(char *r,int *sa,int n,int m){    int i,j,p,*x=wa,*y=wb,*t;    for(i=0; i<m; i++) wss[i]=0;    for(i=0; i<n; i++) wss[x[i]=(int)r[i]]++;    for(i=1; i<m; i++) wss[i]+=wss[i-1];    for(i=n-1; i>=0; i--) sa[--wss[x[i]]]=i;    for(j=1,p=1; p<n; j*=2,m=p)    {        for(p=0,i=n-j; i<n; i++) y[p++]=i;        for(i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;        for(i=0; i<n; i++) wv[i]=x[y[i]];        for(i=0; i<m; i++) wss[i]=0;        for(i=0; i<n; i++) wss[wv[i]]++;        for(i=1; i<m; i++) wss[i]+=wss[i-1];        for(i=n-1; i>=0; i--) sa[--wss[wv[i]]]=y[i];        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;    }    return;}void callheight(char *r,int *sa,int n){    int i,j,k=0;    for(i=1;i<=n;i++)        ran[sa[i]]=i;    for(i=0;i<n;height[ran[i++]]=k)    for(k?k--:0,j=sa[ran[i]-1];r[i+k]==r[j+k];k++);    return ;}void init(int len){    for(int i=1;i<=len;i++) f[i][0]=height[i];    for(int s=1;(1<<s)<=len;s++)    {        int tmp=(1<<s);        for(int i=1;i+tmp-1<=len;i++)        {            f[i][s]=min(f[i][s-1],f[i+tmp/2][s-1]);        }    }}int cal(int l,int r){    int len=log2(r-l+1);    int ans=min(f[l][len],f[r-(1<<len)+1][len]);    return ans;}int main(){    int T; cin>>T;    while(T--)    {       scanf("%d%s",&k,s);       int len=strlen(s);       da(s,sa,len+1,130);       callheight(s,sa,len);       init(len);       int ans=0;       for(int i=1;i+k-1<=len;i++)       {           int j=i+k-1;           int tmp=height[i];           if(j+1<=len) tmp=max(tmp,height[j+1]);           int x;           if(k!=1) { x=cal(i+1,j); }           else x=len-sa[i];           ans+=max(0,x-tmp);       }       printf("%d\n",ans);    }    return 0;}

 

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.