Description
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
abababSample Output
1 1 6 1
1 6 1 6
1 3 2 3
【题目大意】
给定一些环,如果按照顺时针的方式破环为链,那么最大的字符串,最小的字符串出现的位置(最小的)以及出现的次数。
【题目分析】
字符串的最大/最小表示法。因为最大最小状态出现的次数是比较好解决的,只需要求出完整的循环节的个数就可以了。而最大最小表示法的方法比较巧妙,建议读者查找相关资料自行解决,这里不再赘述。方法的大意就是排除所有不可能是结果的情况,那么剩下的就是最终的答案了。
【代码】
(略丑,因为把最大最小表示法写到了一起,比较难看,但是十分简短)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char a[1000001];
int ne[1000001];int l,len,ans,minn,maxx;
inline int query(int flag)
{
int i=0,j=1,k=0;
while (i<=l&&j<=l&&k<=l)
{
int t=a[(i+k)%l+1]-a[(j+k)%l+1];
if (t==0) k++;
else
{
if (flag){
if (t>0) j+=k+1;
else i+=k+1;
}
else {
if (t>0) i+=k+1;
else j+=k+1;
}
if (i==j) j++;
k=0;
}
}
return min(i,j);
}
int main()
{
while (scanf("%s",a+1)!=EOF)
{
l=strlen(a+1);
for (int i=2,j=0;i<=l;++i)
{
while (j&&a[j+1]!=a[i]) j=ne[j];
if (a[j+1]==a[i]) j++;
ne[i]=j;
}
int len=l-ne[l];
int ans=l%len?1:l/len;
int minn=query(1),maxx=query(0);
printf("%d %d %d %d\n",maxx+1,ans,minn+1,ans);
}
}