最小表示法+KMP
String Problem
Give you a string with length N, you can generate N strings by left shifts. For example let consider the string “SKYLONG”, we can generate seven strings:
String Rank
SKYLONG 1
KYLONGS 2
YLONGSK 3
LONGSKY 4
ONGSKYL 5
NGSKYLO 6
GSKYLON 7
and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once.
Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.
Input
Each line contains one line the string S with length N (N <= 1000000) formed by lower case letters.
Output
Output four integers separated by one space, lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), the string’s times in the N generated strings, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.
Sample Input
abcder aaaaaa ababab
Sample Output
1 1 6 1 1 6 1 6 1 3 2 3
#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
#include<set>
#include<queue>
using namespace std;
char ss[1000005];
int net[1000005];
int getminindex()
{
int len=strlen(ss);
int i=0,j=1,k=0;
while(i<len&&j<len&&k<len){
if(ss[(i+k)%len]==ss[(j+k)%len])
k++;
else if(ss[(i+k)%len]<ss[(j+k)%len]){
j=j+k+1;
k=0;
}
else{
i=i+k+1;
k=0;
}
if(i==j)
j++;
}
return i<j?i:j;
}
int getmaxindex()
{
int len=strlen(ss);
int i=0,j=1,k=0;
while(i<len&&j<len&&k<len){
if(ss[(i+k)%len]==ss[(j+k)%len])
k++;
else if(ss[(i+k)%len]>ss[(j+k)%len]){
j=j+k+1;
k=0;
}
else{
i=i+k+1;
k=0;
}
if(i==j)
j++;
}
return i<j?i:j;
}
void getnet()
{
int len=strlen(ss);
for(int i=1;i<len;i++){
int j=net[i-1];
while(ss[j]!=ss[i]&&j>0)
j=net[j-1];
if(ss[j]==ss[i])
net[i]=j+1;
else
net[i]=0;
}
}
int main()
{
while(scanf("%s",ss)!=-1){
getnet();
int len=strlen(ss);
int mincycle=len-net[len-1];
int m=1;
if(len%mincycle==0)
m=len/mincycle;
printf("%d %d %d %d\n",getminindex()+1,m,getmaxindex()+1,m);
}
return 0;
}
Manacher
T - Palindrome
Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"
A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.
The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".
If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
Input
Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity).
Output
For each test case in the input print the test case number and the length of the largest palindrome.
Sample Input
abcbabcbabcba abacacbaaaab END
Sample Output
Case 1: 13 Case 2: 6
#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
#include<set>
#include<queue>
#include<map>
#include<iostream>
#include<string>
using namespace std;
const int num=1000010;
int r[num*2];
char ss[num],ssa[num*2];
int get_r()
{
ssa[0]='#';
int len=1;
int lenss=strlen(ss);
for(int i=0; i<lenss; i++)
{
ssa[len++]=ss[i];
ssa[len++]='#';
}
ssa[len]='\0';
int bound=0,cent=0,maxr=0;
for(int i=0; i<len; i++)
{
if(i<bound)
r[i]=min(r[cent*2-i],bound-i);
else
r[i]=0;
while(i-r[i]-1>=0&&i+r[i]+1<len&&ssa[i-r[i]-1]==ssa[i+r[i]+1])
r[i]++;
if(bound<i+r[i])
{
cent=i;
bound=i+r[i];
}
maxr=max(maxr,r[i]);
}
return maxr;
}
int main()
{
int cnt=1;
while(scanf("%s",ss)!=-1)
{
if(ss[0]=='E')
break;
memset(r,0,sizeof(r));
printf("Case %d: %d\n",cnt++,get_r());
}
return 0;
}
扩展KMP
const int maxn=100010;
int net[maxn],ex[maxn];
void getnet(char* s2)
{
int i=0,j,po,len=strlen(s2);
net[0]=len;
while(s2[i]==s2[i+1]&&i+1<len)
i++;
net[1]=i;
po=1;
for(i=2; i<len; i++){
if(net[i-po]+i<net[po]+po)
net[i]=net[i-po];
else{
j=net[po]+po-i;
if(j<0)
j=0;
while(i+j<len&&s2[j]==s2[j+i])
j++;
net[i]=j;
po=i;
}
}
}
void getex(char* s1,char* s2)
{
int i=0,j,po,len1=strlen(s1),len2=strlen(s2);
getnet(s2);
while(s1[i]==s2[i]&&i<len1&&i<len2)
i++;
ex[0]=i;
po=0;
for(i=1; i<len1; i++)
{
if(net[i-po]+i<ex[po]+po)
ex[i]=net[i-po];
else{
j=ex[po]+po-i;
if(j<0)
j=0;
while(i+j<len1&&j<len2&&s1[j+i]==s2[j])
j++;
ex[i]=j;
po=i;
}
}
}