#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
void getNext(char *str,int *next)
{
int j = 0;
int len = strlen(str);
next[1] = 0;
for(int i = 2;i<len+1;i++)
{
while(j>0&&str[j]!=str[i-1])
{
j = next[j];
}
if(str[j]==str[i-1])
j++;
next[i] = j;
}
}
int min_max(char *str,bool flag)
{
int len = strlen(str);
int p1=0,p2=1,k=0;
while(p1<len&&p2<len&&k<len)
{
int c = str[(p2+k)%len]-str[(p1+k)%len];
if(c == 0)
{
k++;
}
else
{
if(flag)
{
if(c>0)
p2+=k+1;
else
p1+=k+1;
}
else
{
if(c>0)
p1+=k+1;
else
p2+=k+1;
}
if(p1==p2)
p2++;
k = 0;
}
}
return min(p1,p2);
}
char str[1000005];
int next[1000005];
int main()
{
while(scanf("%s",str)!=EOF)
{
int minstart = min_max(str,true)+1;
int maxstart = min_max(str,false)+1;
int len = strlen(str);
getNext(str,next);
int tmp = len - next[len];
int ans = len%tmp==0?len/tmp:1;
printf("%d %d %d %d\n",minstart,ans,maxstart,ans);
}
return 0;
}
最大最小表示获得字典顺序的字符串下标,KMP获得次数。
参见http://blog.csdn.net/acm_cxlove/article/details/7909087