题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374
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
题目翻译:
给你一个长度为N的字符串,您可以通过左移生成 N 字符串。例如,
让我们考虑字符串"SKYLONG",我们可以生成七个字符串: 字符串排名
SKYLONG
1 KYLONGs 2
YLONGSK
3 龙基 4
ONGSKYL 5
NGSKYLO 6 GSKYLON 7
而字典学第一是GSKYLON,字典上最后是YLONGSK,两者只出现过一次。
您的任务很简单,计算字典图形化的 fisrt 字符串的等级(如果有多个答案,请选择最小的答案),其时间,按字典顺序排列的最后一个字符串的等级(如果有多个答案,请选择最小的答案),并计算其时间也。
输入
每行包含一行字符串 S 的长度为 N (N <= 100000), 由小写字母组成。
输出
输出四个整数,由一个空格分隔,字典学词字符串的等级(如果有多个答案,请选择最小的答案),字符串在 N 生成的字符串中的时间,按字典顺序最后一个字符串的"排名"(如果有多个答案),选择最小的一个),它的时间也。
最大最小表示法裸题+循环节
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
string s;
int nxt[1000005];
void getnext(string str){
int i = 0, j = -1, len = str.size();
nxt[0] = -1;
while(i < len){
if(j == -1 || str[i] == str[j])
nxt[++i] = ++j;
else
j = nxt[j];
}
}
int posmin(int len){
int i=0,j=1,k=0;
while(i<len&&j<len&&k<len){
int pan=s[(i+k)%len]-s[(j+k)%len];
if(!pan)
k++;
else{
if(pan>0)
i+=k+1;
else
j+=k+1;
if(i==j)
j++;
k=0;
}
}
return min(i+1,j+1);
}
int posmax(int len){
int i=0,j=1,k=0;
while(i<len&&j<len&&k<len){
int pan=s[(i+k)%len]-s[(j+k)%len];
if(!pan)
k++;
else{
if(pan>0)
j+=k+1;
else
i+=k+1;
if(i==j)
j++;
k=0;
}
}
return min(i+1,j+1);
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
while(cin>>s){
int cnt = 1;
int len = s.size();
getnext(s);
if(len%(len-nxt[len])==0)
cnt = len/(len-nxt[len]);
cout << posmin(len) << ' ' << cnt << ' ' << posmax(len) << ' ' << cnt << endl;
}
}