台州学院we are without brain 训练 后缀数组
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
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 last test case is followed by one zero.
Output
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
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
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
Lines 2.. N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.
Output
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
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
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
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
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
A substring of a string T is defined as:
T( i, k)= TiTi +1... Ti+k -1, 1≤ i≤ i+k-1≤| T|.
Given two strings A, B and one integer K, we define S, a set of triples (i, j, k):
S = {( i, j, k) | k≥ K, A( i, k)= B( j, k)}.
You are to give the value of |S| for specific A, B 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 训练 后缀数组的更多相关文章
- 台州学院we are without brain 训练 计算几何
A - View Angle Flatland has recently introduced a new type of an eye check for the driver's licence. ...
- 【UVA11107 训练指南】Life Forms【后缀数组】
题意 输入n(n<=100)个字符串,每个字符串长度<=1000,你的任务是找出一个最长的字符串使得超过一半的字符串都包含这个字符串. 分析 训练指南上后缀数组的一道例题,据说很经典(估计 ...
- 2016多校联合训练4 F - Substring 后缀数组
Description ?? is practicing his program skill, and now he is given a string, he has to calculate th ...
- 牛客网多校训练第一场 I - Substring(后缀数组 + 重复处理)
链接: https://www.nowcoder.com/acm/contest/139/I 题意: 给出一个n(1≤n≤5e4)个字符的字符串s(si ∈ {a,b,c}),求最多可以从n*(n+1 ...
- Hash(LCP) || 后缀数组 LA 4513 Stammering Aliens
题目传送门 题意:训练指南P225 分析:二分寻找长度,用hash值来比较长度为L的字串是否相等. #include <bits/stdc++.h> using namespace std ...
- 后缀数组--summer-work之我连模板题都做不起
这章要比上章的AC自动机要难理解. 这里首先要理解基数排序:基数排序与桶排序,计数排序[详解] 下面通过这个积累信心:五分钟搞懂后缀数组!后缀数组解析以及应用(附详解代码) 下面认真研读下这篇: [转 ...
- 后缀数组的倍增算法(Prefix Doubling)
后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
随机推荐
- 用到UdpClient的一点经验
Thread.Abort对UdpClient.Receive阻塞的线程无效 http://computer-programming-forum.com/4-csharp/184f9d4ee63704f ...
- Ubuntu ndk环境变量配置
https://blog.csdn.net/gulingfengze/article/details/70149092 用source /etc/profile,有些博客写的使用sudo gedit ...
- lasagne保存网络参数
# Optionally, you could now dump the network weights to a file like this: # np.savez('model.npz', *l ...
- JQuery基础原理 与实例 验证表单 省市联动 文本框判空 单选 复选 判空 下拉判空 确认密码判等
JQuery 基础原理 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- java 打印流 递归复制子文件子文件夹 不同编码文件复制到同一文件中 序列化流反序列化流
package com.swift.jinjie; import java.io.BufferedInputStream; import java.io.File; import java.io.Fi ...
- C#把日期转化成星期
显示效果: ***** 前台页面代码: <TextBlock Grid.Row="/> <TextBlock Grid.Row="/> ...
- 【学时总结】 ◆学时·IV◆ 数位DP
[学时·IV] 数位DP ■基本策略■ 说白了就是超时和不超时的区别 :) 有一些特别的题与数位有关,但是用一般的枚举算法会超时.这时候就有人提出了--我们可以用动态规划!通过数字前一位和后一位之间的 ...
- vim 个性化设置和操作
一.vim 设置 1. 设置行号显示 1) 临时显示 命令行模式 :set nu 2) 永久显示 # vim ~/.vimrc 插入一行代码: set number 若没有该文件,在用户主目录 (/h ...
- selenium webdriver 移动到某个位置,滚动到某个位置
https://blog.csdn.net/xiaosongbk/article/details/70231564
- linux基础目录
第1章 linux目录结构 1.1 linux目录结构的特点 一切皆文件 1)倒挂的树状结构 一切从根开始 2)linux每个目录可以挂载在不同的设备(磁盘)上.windows不容易做到. /da ...