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

rank[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+;
int len,llen,p[N+N],pp[N],ans;
char s[N],ss[N+N];
void manacher()
{
int id=,mx=;
for(int i=; i<=llen; i++)
{
if(mx>i)
p[i]=min(p[*id-i],mx-i);
else
p[i]=;
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,,sizeof(p));
len=strlen(s),llen=*len+;
ans=;
for(int i=; i<=len; i++)
ss[*i+]=s[i],ss[*i+]='#';
ss[]='&';
manacher();
for(int i=; i<=llen; i++)
{
if(i+p[i]-==llen)
{
for(int k=; k<=llen; k++)
if(ss[k]!='#')printf("%c",ss[k]);
for(int k=i-p[i]+; k>=; k--)
if(ss[k]!='#')printf("%c",ss[k]);
putchar();
break;
}
}
}
return ;
}

KMP的AC代码

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
char s[N],t[N];
int nxt[N];
void pre(char *t)
{
int i=,j=-;
nxt[]=-;
while(t[i])
{
if(j==-||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=,j=;
while(s[i])
{
if(j==-||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=; i<l; i++) t[i]=s[l--i];t[l]=;
printf("%s%s\n",s,&t[KMP(s,t)]);
}
return ;
}

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:

  • is at least five notes long
  • appears (potentially transposed -- see below) again somewhere else in the piece of music
  • is disjoint from (i.e., non-overlapping with) at least one of its other appearance(s)

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=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[x[i]=r[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,i=n-j; i<n; i++) y[p++]=i;
for(i=; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=; i<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[wv[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
int i,j,k=;
for(i=; i<=n; i++) rank[sa[i]]=i;
for(i=; i<n; height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-]; r[i+k]==r[j+k]; k++);
}
int check(int *sa,int n,int k)
{
int i,ma=sa[],mi=sa[];
for(i=; 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 ;
}
}
return ;
}
int r[maxn],sa[maxn];
int main()
{
int n,y;
while(~scanf("%d",&n),n)
{
n--,scanf("%d",&y);
for(int i=,x; i<n; i++)scanf("%d",&x),r[i]=y-x+,y=x;
r[n]=;
getsa(r,sa,n+,);
calheight(r,sa,n);
int L=,R=n/,mi;
while(L<=R)
{
mi=L+R>>;
if(check(sa,n,mi)) L=mi+;
else R=mi-;
}
printf("%d\n",R<?:R+);
}
return ;
}

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=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[x[i]=r[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,i=n-j; i<n; i++) y[p++]=i;
for(i=; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=; i<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[wv[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
int i,j,k=;
for(i=; i<=n; i++) rank[sa[i]]=i;
for(i=; i<n; height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-]; r[i+k]==r[j+k]; k++);
}
int check(int *sa,int n,int k)
{
int i,mi=sa[],cnt=;
for(i=; i<=n; i++)
{
//dbg(height[i]);
if(height[i]>=k)
{
cnt++;
mi=std::min(mi,sa[i]);
}
else
{
cnt=;
mi=sa[i];
}
if(cnt>=m)return ;
}
return ;
}
int r[maxn],sa[maxn];
int main()
{
int n;
while(~scanf("%d%d",&n,&m))
{
int ma=;
for(int i=;i<n;i++)
scanf("%d",&r[i]),ma=std::max(ma,r[i]);
r[n]=;
getsa(r,sa,n+,ma+);
calheight(r,sa,n);
int L=,R=n,mi;
while(L<=R)
{
mi=L+R>>;
if(check(sa,n,mi))L=mi+;
else R=mi-;
}
printf("%d\n",R);
}
return ;
}

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=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[x[i]=r[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,i=n-j; i<n; i++) y[p++]=i;
for(i=; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=; i<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[wv[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
int i,j,k=;
for(i=; i<=n; i++) rank[sa[i]]=i;
for(i=; i<n; height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-]; r[i+k]==r[j+k]; k++);
}
int check(int *sa,int n,int ans)
{
for(int i=; 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=;
for(int i=;str[i];i++)
r[i]=str[i],n++;
r[n]=;
getsa(r,sa,n+,);
calheight(r,sa,n);
printf("%d\n",check(sa,n,n*(n+)/));
}
return ;
}

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=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[x[i]=r[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,i=n-j; i<n; i++) y[p++]=i;
for(i=; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=; i<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[wv[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
int i,j,k=;
for(i=; i<=n; i++) rank[sa[i]]=i;
for(i=; i<n; height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-]; r[i+k]==r[j+k]; k++);
}
int check(int *sa,int n,int ans)
{
for(int i=; 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=;
for(int i=; str[i]; i++)
r[i]=str[i],n++;
r[n]=;
getsa(r,sa,n+,);
calheight(r,sa,n);
printf("%d\n",check(sa,n,n*1LL*(n+)/));
}
return ;
}

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=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[x[i]=r[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,i=n-j; i<n; i++) y[p++]=i;
for(i=; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=; i<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[wv[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
int i,j,k=;
for(i=; i<=n; i++) rank[sa[i]]=i;
for(i=; i<n; height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-]; r[i+k]==r[j+k]; k++);
}
int check(int *sa,int n,int ans)
{
for(int i=; i<=n; i++)
{
//dbg(height[i]);
ans-=height[i];
}
return ans;
}
int r[maxn],sa[maxn];
char str[maxn];
int f[maxn],dmi[maxn][];
void RMQ_init(int n)
{
f[]=-;
for(int i=; i<=n; i++)
dmi[i][]=height[i],f[i]=((i&(i-))==)?f[i-]+:f[i-];
for(int j=; (<<j)<=n; j++)
for(int i=; i+j-<=n; i++)
dmi[i][j]=std::min(dmi[i][j-],dmi[i+(<<(j-))][j-]);
}
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+];
return std::min(dmi[l][k],dmi[r-(<<k)+][k]);
}
int main()
{
int n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=; i<n; i++)scanf("%s",str+i),r[i]=str[i];
r[n]=;
getsa(r,sa,n+,);
calheight(r,sa,n);
RMQ_init(n);
int ans=;
for(int i=; i<n; i++)
for(int j=; j+i<n; j+=i)
{
int k=lcp(j,j+i),now=k/i,tj=j-(i-k%i);
if(tj>=)
{
if(lcp(tj,tj+i)>=i-k%i)now++;
}
ans=std::max(ans,now);
}
printf("%d\n",ans+);
}
return ;
}

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

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=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[x[i]=r[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,i=n-j; i<n; i++) y[p++]=i;
for(i=; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=; i<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) ws[i]=;
for(i=; i<n; i++) ws[wv[i]]++;
for(i=; i<m; i++) ws[i]+=ws[i-];
for(i=n-; i>=; i--) sa[--ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
int i,j,k=;
for(i=; i<n; i++) rank[sa[i]]=i;
for(i=; i<n; height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-]; 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,,sizeof rank);
getsa(r,sa,n,);
calheight(r,sa,n);
long long res=;
for(int i=,j; i<n; i++)
if(height[i]>=K)
{
st[]=i-,st[top=]=i,num[]=height[i]-K+,res+=height[i]-K+;
for(j=i+; j<n&&height[j]>=K; j++)
{
while(top&&height[j]<=height[st[top]])top--;
st[++top]=j,num[top]=num[top-]+(j-st[top-])*1LL*(height[j]-K+),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=;
scanf("%s",str1);
for(int i=; str1[i]; i++)r[n++]=str1[i];
r[n++]=;
scanf("%s",str2);
for(int i=; str2[i]; i++)r[n++]=str2[i];
r[n++]='';
ans=la(r,sa,n,k);
n=;
for(int i=; str1[i]; i++)r[n++]=str1[i];
r[n++]='';
ans-=la(r,sa,n,k);
n=;
for(int i=; str2[i]; i++)r[n++]=str2[i];
r[n++]='';
ans-=la(r,sa,n,k);
printf("%lld\n",ans);
}
return ;
}

台州学院we are without brain 训练 后缀数组的更多相关文章

  1. 台州学院we are without brain 训练 计算几何

    A - View Angle Flatland has recently introduced a new type of an eye check for the driver's licence. ...

  2. 【UVA11107 训练指南】Life Forms【后缀数组】

    题意 输入n(n<=100)个字符串,每个字符串长度<=1000,你的任务是找出一个最长的字符串使得超过一半的字符串都包含这个字符串. 分析 训练指南上后缀数组的一道例题,据说很经典(估计 ...

  3. 2016多校联合训练4 F - Substring 后缀数组

    Description ?? is practicing his program skill, and now he is given a string, he has to calculate th ...

  4. 牛客网多校训练第一场 I - Substring(后缀数组 + 重复处理)

    链接: https://www.nowcoder.com/acm/contest/139/I 题意: 给出一个n(1≤n≤5e4)个字符的字符串s(si ∈ {a,b,c}),求最多可以从n*(n+1 ...

  5. Hash(LCP) || 后缀数组 LA 4513 Stammering Aliens

    题目传送门 题意:训练指南P225 分析:二分寻找长度,用hash值来比较长度为L的字串是否相等. #include <bits/stdc++.h> using namespace std ...

  6. 后缀数组--summer-work之我连模板题都做不起

    这章要比上章的AC自动机要难理解. 这里首先要理解基数排序:基数排序与桶排序,计数排序[详解] 下面通过这个积累信心:五分钟搞懂后缀数组!后缀数组解析以及应用(附详解代码) 下面认真研读下这篇: [转 ...

  7. 后缀数组的倍增算法(Prefix Doubling)

    后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...

  8. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  9. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

随机推荐

  1. 动画利器animate.css

    使用过CSS3编写动画的同学一定感叹CSS3的强大,但是也会感到书写的麻烦.每次都要计算动画的各个参数,十分麻烦.有没有一个库能封装一些常用的CSS3动画效果.答案是肯定的,animate.css就是 ...

  2. rnn,lstm and JuergenSchmidhuber

    JuergenSchmidhuber 是瑞士的一位牛人,主要贡献是rnn, lstm. google的deep mind新作,Human-level control through deep rein ...

  3. 九九乘法表(Python实现)

    a = 1 #while实现 while a: b = 1 while b: print(str(b)+'*'+str(a),end='=') print(a*b,end=' ') if b == a ...

  4. 网上商城_数据库jar包的使用

    网上商城_数据库jar包的使用 0.导入数据库相关jar包 commons-dbutils-1.4.jar c3p0-0.9.1.2.jar 1.配置C3P0-config.xml文件 <?xm ...

  5. django+xadmin在线教育平台(十三)

    这个6-8对应对应6-11,6-12 拷入forgetpassword页面 书写处理忘记密码的view users/views.py # 用户忘记密码的处理view class ForgetPwdVi ...

  6. sqlite的sql常用语句(笔记)

    1.复制一张表并重命名 比如已经创建好一个表 表名为"28165" 复制这个表. CREATE TABLE [33150] AS SELECT * FROM [28165] 2.根 ...

  7. JavaScript---DOM对象(DHTML)

    1.什么是DOM? DOM 是 W3C(万维网联盟)的标准.DOM 定义了访问 HTML 和 XML 文档的标准: "W3C 文档对象模型(DOM)是中立于平台和语言的接口,它允许程序和脚本 ...

  8. 两种js方法发起微信支付:WeixinJSBridge,wx.chooseWXPay区别

    原文链接:https://www.2cto.com/weixin/201507/412752.html 1.为什么会有两种JS方法可以发起微信支付? 当你登陆微信公众号之后,左边有两个菜单栏,一个是微 ...

  9. (转)零基础学习 Hadoop 该如何下手?

    推荐一些Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeeper, Avro, Amb ...

  10. ubuntu中使用apt命令安装ipython失败解决方案

    在最近使用ubuntu安装ipython时,出现如下报错: 出现这个问题,主要是因为apt还在运行,故解决方案为: 1.找到并且杀掉所有的apt-get 和apt进程 运行下面的命令来生成所有含有 a ...