其他分享
首页 > 其他分享> > 台州学院we are without brain 训练 后缀数组

台州学院we are without brain 训练 后缀数组

作者:互联网

sa[i]表示排名为 i 的后缀的第一个字符在原串中的位置

height[i]表示排名为 i 和排名为 i+1的后缀的最长公共前缀的长度

 

这些题目我并不一定全是用SA做的,但是还是要标记一下的

K - Extend to Palindrome

Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse it) of length at least N. Any palindrome will do. Easy, isn’t it? That’s what you thought before you passed it on to your inexperienced team-mate. When the contest is almost over, you find out that that problem still isn’t solved. The problem with the code is that the strings generated are often not palindromic. There’s not enough time to start again from scratch or to debug his messy code. Seeing that the situation is desperate, you decide to simply write some additional code that takes the output and adds just enough extra characters to it to make it a palindrome and hope for the best. Your solution should take as its input a string and produce the smallest palindrome that can be formed by adding zero or more characters at its end. Input Input will consist of several lines ending in EOF. Each line will contain a non-empty string made up of upper case and lower case English letters (‘A’-‘Z’ and ‘a’-‘z’). The length of the string will be less than or equal to 100,000. Output For each line of input, output will consist of exactly one line. It should contain the palindrome formed by adding the fewest number of extra letters to the end of the corresponding input string.

Sample Input

aaaa

abba

amanaplanacanal

xyz

Sample Output

aaaa

abba

amanaplanacanalpanama

xyzyx

在原串上加入最少的字符使其变为回文子串

可以这样考虑,最多就是把这个字串逆置之后全部加上,或者加上逆置之后的前面一段,所以预处理之后跑一下马拉车

还可以将串倒置求KMP的最长匹配,这个想法还是很明显的

马拉车AC代码

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int len,llen,p[N+N],pp[N],ans;
char s[N],ss[N+N];
void manacher()
{
    int id=0,mx=0;
    for(int i=2; i<=llen; i++)
    {
        if(mx>i)
            p[i]=min(p[2*id-i],mx-i);
        else
            p[i]=1;
        while(ss[i+p[i]]==ss[i-p[i]]) p[i]++;
        if(p[i]+i>mx)mx=p[i]+i,id=i;
        ans=max(ans,p[i]);
    }
}
int main()
{
    while(~scanf("%s",s))
    {
        memset(p,0,sizeof(p));
        len=strlen(s),llen=2*len+1;
        ans=0;
        for(int i=0; i<=len; i++)
            ss[2*i+2]=s[i],ss[2*i+1]='#';
        ss[0]='&';
        manacher();
        for(int i=1; i<=llen; i++)
        {
            if(i+p[i]-1==llen)
            {
                for(int k=1; k<=llen; k++)
                    if(ss[k]!='#')printf("%c",ss[k]);
                for(int k=i-p[i]+1; k>=1; k--)
                    if(ss[k]!='#')printf("%c",ss[k]);
                putchar(10);
                break;
            }
        }
    }
    return 0;
}

KMP的AC代码

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
char s[N],t[N];
int nxt[N];
void pre(char *t)
{
    int i=0,j=-1;
    nxt[0]=-1;
    while(t[i])
    {
        if(j==-1||t[i]==t[j])
        {
            i++,j++;
            if(t[i]!=t[j])nxt[i]=j;
            else nxt[i]=nxt[j];
        }
        else j=nxt[j];
    }
}
int KMP(char *s,char *t)
{
    pre(t);
    int i=0,j=0;
    while(s[i])
    {
        if(j==-1||s[i]==t[j])i++,j++;
        else j=nxt[j];
    }
    return j;
}
int main()
{
    while(~scanf("%s",s))
    {
        int l=strlen(s);
        for(int i=0; i<l; i++) t[i]=s[l-1-i];t[l]=0;
        printf("%s%s\n",s,&t[KMP(s,t)]);
    }
    return 0;
}

SA的做法会比较麻烦的吧

SA找两个串的最长公共子串限定这个子串的范围

A - Musical Theme

 

A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1..88, each representing a key on the piano. It is unfortunate but true that this representation of melodies ignores the notion of musical timing; but, this programming task is about notes and not timings. 
Many composers structure their music around a repeating &qout;theme&qout;, which, being a subsequence of an entire melody, is a sequence of integers in our representation. A subsequence of a melody is a theme if it: 

Transposed means that a constant positive or negative value is added to every note value in the theme subsequence. 
Given a melody, compute the length (number of notes) of the longest theme. 
One second time limit for this problem's solutions! 

Input

The input contains several test cases. The first line of each test case contains the integer N. The following n integers represent the sequence of notes. 
The last test case is followed by one zero. 

Output

For each test case, the output file should contain a single line with a single integer that represents the length of the longest theme. If there are no themes, output 0.

Sample Input

30
25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18
82 78 74 70 66 67 64 60 65 80
0

Sample Output

5

Hint

Use scanf instead of cin to reduce the read time.

 n个音乐符构成了一个调调,然后这个调调有各种各样的规则

二分ans就好了

#include "stdio.h"
#define maxn 20000
int wa[maxn],wb[maxn],wv[maxn],ws[maxn];
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void getsa(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++) ws[i]=0;
    for(i=0; i<n; i++) ws[x[i]=r[i]]++;
    for(i=1; i<m; i++) ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--) sa[--ws[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++) ws[i]=0;
        for(i=0; i<n; i++) ws[wv[i]]++;
        for(i=1; i<m; i++) ws[i]+=ws[i-1];
        for(i=n-1; i>=0; i--) sa[--ws[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++;
    }
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++) rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
}
int check(int *sa,int n,int k)
{
    int i,ma=sa[1],mi=sa[1];
    for(i=2; i<=n; i++)
    {
        if(height[i]<k) ma=mi=sa[i];
        else
        {
            if(sa[i]<mi) mi=sa[i];
            if(sa[i]>ma) ma=sa[i];
            if(ma-mi>k) return 1;
        }
    }
    return 0;
}
int r[maxn],sa[maxn];
int main()
{
    int n,y;
    while(~scanf("%d",&n),n)
    {
        n--,scanf("%d",&y);
        for(int i=0,x; i<n; i++)scanf("%d",&x),r[i]=y-x+100,y=x;
        r[n]=0;
        getsa(r,sa,n+1,200);
        calheight(r,sa,n);
        int L=1,R=n/2,mi;
        while(L<=R)
        {
            mi=L+R>>1;
            if(check(sa,n,mi)) L=mi+1;
            else R=mi-1;
        }
        printf("%d\n",R<4?0:R+1);
    }
    return 0;
}

 

B - Milk Patterns

 POJ - 3261

Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can't predict the quality of milk from one day to the next, there are some regular patterns in the daily milk quality.

To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has recorded data from a single cow over N (1 ≤ N ≤ 20,000) days. He wishes to find the longest pattern of samples which repeats identically at least K (2 ≤ K ≤ N) times. This may include overlapping patterns -- 1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.

Help Farmer John by finding the longest repeating subsequence in the sequence of samples. It is guaranteed that at least one subsequence is repeated at least Ktimes.

Input

Line 1: Two space-separated integers: N and K 
Lines 2.. N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.

Output

Line 1: One integer, the length of the longest pattern which occurs at least K times

Sample Input

8 2
1
2
3
2
3
2
3
1

Sample Output

4

找出出现k次的可重叠的最长子串的长度

继续二分

#include <stdio.h>
#include <iostream>
#include <algorithm>
#define dbg(x) std::cout<<#x<<" = "<< (x)<< "\n"
#define maxn 20005
int wa[maxn],wb[maxn],wv[maxn],ws[maxn],m;
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void getsa(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++) ws[i]=0;
    for(i=0; i<n; i++) ws[x[i]=r[i]]++;
    for(i=1; i<m; i++) ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--) sa[--ws[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++) ws[i]=0;
        for(i=0; i<n; i++) ws[wv[i]]++;
        for(i=1; i<m; i++) ws[i]+=ws[i-1];
        for(i=n-1; i>=0; i--) sa[--ws[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++;
    }
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++) rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
}
int check(int *sa,int n,int k)
{
    int i,mi=sa[1],cnt=1;
    for(i=2; i<=n; i++)
    {
        //dbg(height[i]);
        if(height[i]>=k)
        {
            cnt++;
            mi=std::min(mi,sa[i]);
        }
        else
        {
            cnt=1;
            mi=sa[i];
        }
        if(cnt>=m)return 1;
    }
    return 0;
}
int r[maxn],sa[maxn];
int main()
{
    int n;
    while(~scanf("%d%d",&n,&m))
    {
        int ma=0;
        for(int i=0;i<n;i++)
            scanf("%d",&r[i]),ma=std::max(ma,r[i]);
        r[n]=0;
        getsa(r,sa,n+1,ma+1);
        calheight(r,sa,n);
        int L=1,R=n,mi;
        while(L<=R)
        {
            mi=L+R>>1;
            if(check(sa,n,mi))L=mi+1;
            else R=mi-1;
        }
        printf("%d\n",R);
    }
    return 0;
}

C - Distinct Substrings

 SPOJ - DISUBSTR 

Given a string, we need to find the total number of its distinct substrings.

Input

T- number of test cases. T<=20;
Each test case consists of one string, whose length is <= 1000

Output

For each test case output one number saying the number of distinct substrings.

Example

Sample Input:
2
CCCCC
ABABA

Sample Output:
5
9

Explanation for the testcase with string ABABA: 
len=1 : A,B
len=2 : AB,BA
len=3 : ABA,BAB
len=4 : ABAB,BABA
len=5 : ABABA
Thus, total number of distinct substrings is 9.

找出一个序列的所有可能,还是和height有关的

不过这个结论是我猜的

#include <stdio.h>
#include <iostream>
#include <algorithm>
#define dbg(x) std::cout<<#x<<" = "<< (x)<< "\n"
#define maxn 20005
int wa[maxn],wb[maxn],wv[maxn],ws[maxn],m;
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void getsa(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++) ws[i]=0;
    for(i=0; i<n; i++) ws[x[i]=r[i]]++;
    for(i=1; i<m; i++) ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--) sa[--ws[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++) ws[i]=0;
        for(i=0; i<n; i++) ws[wv[i]]++;
        for(i=1; i<m; i++) ws[i]+=ws[i-1];
        for(i=n-1; i>=0; i--) sa[--ws[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++;
    }
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++) rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
}
int check(int *sa,int n,int ans)
{
    for(int i=2; i<=n; i++)
    {
        //dbg(height[i]);
        ans-=height[i];
    }
    return ans;
}
int r[maxn],sa[maxn];
char str[maxn];
int main()
{
    int n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str),n=0;
        for(int i=0;str[i];i++)
            r[i]=str[i],n++;
        r[n]=0;
        getsa(r,sa,n+1,300);
        calheight(r,sa,n);
        printf("%d\n",check(sa,n,n*(n+1)/2));
    }
    return 0;
}

D - New Distinct Substrings

 SPOJ - SUBST1

Given a string, we need to find the total number of its distinct substrings.

Input

T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 50000

Output

For each test case output one number saying the number of distinct substrings.

Example

Input:
2
CCCCC
ABABA

Output:
5
9

D也一样,不过数据更大了

#include <stdio.h>
#include <iostream>
#include <algorithm>
#define dbg(x) std::cout<<#x<<" = "<< (x)<< "\n"
#define maxn 50005
int wa[maxn],wb[maxn],wv[maxn],ws[maxn],m;
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void getsa(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++) ws[i]=0;
    for(i=0; i<n; i++) ws[x[i]=r[i]]++;
    for(i=1; i<m; i++) ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--) sa[--ws[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++) ws[i]=0;
        for(i=0; i<n; i++) ws[wv[i]]++;
        for(i=1; i<m; i++) ws[i]+=ws[i-1];
        for(i=n-1; i>=0; i--) sa[--ws[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++;
    }
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++) rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
}
int check(int *sa,int n,int ans)
{
    for(int i=2; i<=n; i++)
    {
        //dbg(height[i]);
        ans-=height[i];
    }
    return ans;
}
int r[maxn],sa[maxn];
char str[maxn];
int main()
{
    int n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str),n=0;
        for(int i=0; str[i]; i++)
            r[i]=str[i],n++;
        r[n]=0;
        getsa(r,sa,n+1,300);
        calheight(r,sa,n);
        printf("%d\n",check(sa,n,n*1LL*(n+1)/2));
    }
    return 0;
}

E - Repeats

 SPOJ - REPEATS 

A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the string

s = abaabaabaaba

is a (4,3)-repeat with t = aba as its seed string. That is, the seed string t is 3 characters long, and the whole string s is obtained by repeating t 4 times.

Write a program for the following task: Your program is given a long string u consisting of characters ‘a’ and/or ‘b’ as input. Your program must find some (k,l)-repeat that occurs as substring within u with k as large as possible. For example, the input string

u = babbabaabaabaabab

contains the underlined (4,3)-repeat s starting at position 5. Since u contains no other contiguous substring with more than 4 repeats, your program must output the maximum k.

 

Input

In the first line of the input contains H- the number of test cases (H <= 20). H test cases follow. First line of each test cases is n - length of the input string (n <= 50000), The next n lines contain the input string, one character (either ‘a’ or ‘b’) per line, in order.

Output

For each test cases, you should write exactly one interger k in a line - the repeat count that is maximized.

Example

Input:
1
17
b
a
b
b
a
b
a
a
b
a
a
b
a
a
b
a
b

Output:
4

since a (4, 3)-repeat is found starting at the 5th character of the input string.

LCP经典题目,要找最长前缀

#include <stdio.h>
#include <iostream>
#include <algorithm>
#define dbg(x) std::cout<<#x<<" = "<< (x)<< "\n"
#define maxn 53005
int wa[maxn],wb[maxn],wv[maxn],ws[maxn],m;
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void getsa(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++) ws[i]=0;
    for(i=0; i<n; i++) ws[x[i]=r[i]]++;
    for(i=1; i<m; i++) ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--) sa[--ws[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++) ws[i]=0;
        for(i=0; i<n; i++) ws[wv[i]]++;
        for(i=1; i<m; i++) ws[i]+=ws[i-1];
        for(i=n-1; i>=0; i--) sa[--ws[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++;
    }
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++) rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
}
int check(int *sa,int n,int ans)
{
    for(int i=2; i<=n; i++)
    {
        //dbg(height[i]);
        ans-=height[i];
    }
    return ans;
}
int r[maxn],sa[maxn];
char str[maxn];
int f[maxn],dmi[maxn][20];
void RMQ_init(int n)
{
    f[0]=-1;
    for(int i=1; i<=n; i++)
        dmi[i][0]=height[i],f[i]=((i&(i-1))==0)?f[i-1]+1:f[i-1];
    for(int j=1; (1<<j)<=n; j++)
        for(int i=1; i+j-1<=n; i++)
            dmi[i][j]=std::min(dmi[i][j-1],dmi[i+(1<<(j-1))][j-1]);
}
int lcp(int l,int r)
{
    l=rank[l],r=rank[r];
    if(l>r)std::swap(l,r);
    l++;
    int k=f[r-l+1];
    return std::min(dmi[l][k],dmi[r-(1<<k)+1][k]);
}
int main()
{
    int n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)scanf("%s",str+i),r[i]=str[i];
        r[n]=0;
        getsa(r,sa,n+1,300);
        calheight(r,sa,n);
        RMQ_init(n);
        int ans=0;
        for(int i=1; i<n; i++)
            for(int j=0; j+i<n; j+=i)
            {
                int k=lcp(j,j+i),now=k/i,tj=j-(i-k%i);
                if(tj>=0)
                {
                    if(lcp(tj,tj+i)>=i-k%i)now++;
                }
                ans=std::max(ans,now);
            }
        printf("%d\n",ans+1);
    }
    return 0;
}

F - Maximum repetition substring

 POJ - 3693 

The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same consecutive substrings. For example, the repetition number of "ababab" is 3 and "ababa" is 1.

Given a string containing lowercase letters, you are to find a substring of it with maximum repetition number.

Input

The input consists of multiple test cases. Each test case contains exactly one line, which
gives a non-empty string consisting of lowercase letters. The length of the string will not be greater than 100,000.

The last test case is followed by a line containing a '#'.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by the substring of maximum repetition number. If there are multiple substrings of maximum repetition number, print the lexicographically smallest one.

Sample Input

ccabababc
daabbccaa
#

Sample Output

Case 1: ababab
Case 2: aa

继续LCP,不过这个题目我怎么RE了100年

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define dbg(x) std::cout<<#x<<" = "<< (x)<< "\n"
#define maxn 1000005
int wa[maxn],wb[maxn],wv[maxn],ws[maxn],m;
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void getsa(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++) ws[i]=0;
    for(i=0; i<n; i++) ws[x[i]=r[i]]++;
    for(i=1; i<m; i++) ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--) sa[--ws[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++) ws[i]=0;
        for(i=0; i<n; i++) ws[wv[i]]++;
        for(i=1; i<m; i++) ws[i]+=ws[i-1];
        for(i=n-1; i>=0; i--) sa[--ws[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++;
    }
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++) rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
}
int check(int *sa,int n,int ans)
{
    for(int i=2; i<=n; i++)
    {
        //dbg(height[i]);
        ans-=height[i];
    }
    return ans;
}
int r[maxn],sa[maxn];
char str[maxn];
int f[maxn],dmi[maxn][20],a[maxn];
void RMQ_init(int n)
{
    f[0]=-1;
    for(int i=1; i<=n; i++)
        dmi[i][0]=height[i],f[i]=((i&(i-1))==0)?f[i-1]+1:f[i-1];
    for(int j=1; (1<<j)<=n; j++)
        for(int i=1; i+j-1<=n; i++)
            dmi[i][j]=std::min(dmi[i][j-1],dmi[i+(1<<(j-1))][j-1]);
}
int lcp(int l,int r)
{
    l=rank[l],r=rank[r];
    if(l>r)std::swap(l,r);
    l++;
    int k=f[r-l+1];
    return std::min(dmi[l][k],dmi[r-(1<<k)+1][k]);
}
int main()
{
    int ca=0;
    while(~scanf("%s",str))
    {
        int n=0;
        if(strcmp(str,"#")==0)break;
        for(int i=0; str[i]; i++)r[i]=str[i],n++;
        r[n]=0;
        getsa(r,sa,n+1,300);
        calheight(r,sa,n);
        RMQ_init(n);
        int cnt=0,ma=0,len=-1,sbe;
        for(int l=1; l<n; l++)
        {
            for(int i=0; i+l<n; i+=l)
            {
                int k=lcp(i,i+l),now=k/l+1,tj=i-(l-k%l);
                if(tj>=0&&k%l)
                {
                    if(lcp(tj,tj+l)>=k)now++;
                }
                if(now>ma)ma=now,cnt=0,a[cnt++]=l;
                else if(now==ma)a[cnt++]=l;
            }
        }
        for(int i=1; i<=n&&len==-1; i++)
            for(int j=0; j<cnt; j++)
                if(lcp(sa[i],sa[i]+a[j])>=(ma-1)*a[j])
                {
                    len=a[j],sbe=sa[i];
                    break;
                }
        str[sbe+len*ma]=0;
        printf("Case %d: %s\n",++ca,str+sbe);
    }
    return 0;
}

G - Common Substrings

 POJ - 3415 

A substring of a string T is defined as:

 

Tik)= TiTi +1... Ti+k -1, 1≤ i≤ i+k-1≤| T|.

 

Given two strings AB and one integer K, we define S, a set of triples (ijk):

 

S = {( ijk) | k≥ KAik)= Bjk)}.

 

You are to give the value of |S| for specific AB and K.

Input

The input file contains several blocks of data. For each block, the first line contains one integer K, followed by two lines containing strings A and B, respectively. The input file is ended by K=0.

1 ≤ |A|, |B| ≤ 105
1 ≤ K ≤ min{|A|, |B|}
Characters of A and B are all Latin letters.

 

Output

For each case, output an integer |S|.

Sample Input

2
aababaa
abaabaa
1
xx
xx
0

Sample Output

22
5

统计相同子串,要用单调栈去优化

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define dbg(x) std::cout<<#x<<" = "<< (x)<< "\n"
#define maxn 1000005
int wa[maxn],wb[maxn],wv[maxn],ws[maxn],m;
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void getsa(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++) ws[i]=0;
    for(i=0; i<n; i++) ws[x[i]=r[i]]++;
    for(i=1; i<m; i++) ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--) sa[--ws[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++) ws[i]=0;
        for(i=0; i<n; i++) ws[wv[i]]++;
        for(i=1; i<m; i++) ws[i]+=ws[i-1];
        for(i=n-1; i>=0; i--) sa[--ws[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++;
    }
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<n; i++) rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
}
int st[maxn],top;
long long num[maxn],ans;
long long la(int *r,int *sa,int n,int K)
{
    memset(rank,0,sizeof rank);
    getsa(r,sa,n,300);
    calheight(r,sa,n);
    long long res=0;
    for(int i=1,j; i<n; i++)
        if(height[i]>=K)
        {
            st[0]=i-1,st[top=1]=i,num[1]=height[i]-K+1,res+=height[i]-K+1;
            for(j=i+1; j<n&&height[j]>=K; j++)
            {
                while(top&&height[j]<=height[st[top]])top--;
                st[++top]=j,num[top]=num[top-1]+(j-st[top-1])*1LL*(height[j]-K+1),res+=num[top];
            }
            i=j;
        }
    return res;
}
int r[maxn],sa[maxn];
char str1[maxn],str2[maxn];
int main()
{
    int k;
    while(scanf("%d",&k),k)
    {
        int n=0;
        scanf("%s",str1);
        for(int i=0; str1[i]; i++)r[n++]=str1[i];
        r[n++]=180;
        scanf("%s",str2);
        for(int i=0; str2[i]; i++)r[n++]=str2[i];
        r[n++]='0';
        ans=la(r,sa,n,k);
        n=0;
        for(int i=0; str1[i]; i++)r[n++]=str1[i];
        r[n++]='0';
        ans-=la(r,sa,n,k);
        n=0;
        for(int i=0; str2[i]; i++)r[n++]=str2[i];
        r[n++]='0';
        ans-=la(r,sa,n,k);
        printf("%lld\n",ans);
    }
    return 0;
}

 

标签:string,后缀,++,number,brain,int,without,sa,include
来源: https://www.cnblogs.com/BobHuang/p/9137927.html