String Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1512 Accepted Submission(s): 668
Problem 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.
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
这题之前做过一次。今天做的时候还是把最小表示法给搞忘了,用普通枚举求最小字典序,果断超时。
题意:给定一个串。原始串序号为1,每向左移动一次序号加一,求最小字典序的序号和最大字典序的序号以及循环次数。
题解:求循环次数仅仅须要找到循环节点,难点是找最小(大)字典序的序号,要用到最小表示法,这个就当成模板来用吧。最小表示法难理解的地方在s[i+k]>s[j+k]时i须要+=k+1,这是由于s[i...i+k-1]和s[j...j+k-1]相等。说明不论什么以s[i...i+k]開始的字符串都要大于以s[j]開始的字符串。
#include <stdio.h> #define maxn 1000002 char str[maxn]; int next[maxn], len, cir, minPos, maxPos; void getNext() { int i = 0, j = -1; next[0] = -1; while(str[i]){ if(j == -1 || str[i] == str[j]){ ++i; ++j; next[i] = j; //mode 1 }else j = next[j]; } len = i; } void findMinAndMaxPos() { minPos = maxPos = 0; int k = 0, pos = 1, t; while(minPos < len && k < len && pos < len){ t = str[(minPos + k) % len] - str[(pos + k) % len]; if(t == 0){ ++k; continue; } else if(t < 0) pos += k + 1; else minPos += k + 1; k = 0; if(minPos == pos) ++pos; } if(minPos > pos) minPos = pos; ++minPos; k = 0; pos = 1; while(maxPos < len && k < len && pos < len){ t = str[(maxPos + k) % len] - str[(pos + k) % len]; if(t == 0){ ++k; continue; } else if(t > 0) pos += k + 1; else maxPos += k + 1; k = 0; if(maxPos == pos) ++pos; } if(maxPos > pos) maxPos = pos; ++maxPos; } int main() { //freopen("stdin.txt", "r", stdin); while(scanf("%s", str) != EOF){ getNext(); findMinAndMaxPos(); cir = 1; if(len % (len - next[len]) == 0) cir = len / (len - next[len]); printf("%d %d %d %d ", minPos, cir, maxPos, cir); } return 0; }