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
求一个字符串用最大最小表示时的位置和变化中的重复次数,求重复次数就是用next数组的性质,最小最大表示是模板,看明白就好。
/*
模板
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=1000000+10;
char str[MAX];
int Next[MAX];
/*
比找最小表示法,相当于找着一个字符串a,再找一个一直循环比较
如果他是最小的,就会一直增加比较,直到长度为len则返回
如果出现i和j相等的情况,j加1最后返回小的那一个就行
*/
void Get_Next(int len)
{
int i=0,j=-1;
Next[0]=-1;
while(str[i])
{
if(j==-1||str[i]==str[j])
{
i++;
j++;
Next[i]=j;
}
else
j=Next[j];
}
}
int min_max_express(char *s,int len,bool flag) //最小表示法和最大表示法写到一块儿了
{
int i=0,j=1,k=0;
while(i<len&&j<len&&k<len) //一次就能够找着
{
int t=s[(j+k)%len]-s[(i+k)%len];//比较两个字符
if(t==0)
k++;//相等,就后移比较下一个字符
else
{
if(flag) //最小表示法
{
if(t>0)
j+=k+1; //+(k+1)相当于直接把相等的跳过
else
i+=k+1;
}
else //最大表示法
{
if(t<0)
j+=k+1;
else
i+=k+1;
}
if(i==j)
j++;
k=0;
}
}
return min(i,j);
}
int main()
{
while(scanf("%s",str)!=EOF)
{
int len=strlen(str);
int min_express=min_max_express(str,len,true);
int max_express=min_max_express(str,len,false);
Get_Next(len);
int ans=len%(len-Next[len])?1:len/(len-Next[len]);
printf("%d %d %d %d\n",min_express+1,ans,max_express+1,ans);//因为从0开始的,最大最小项+1
}
return 0;
}