String Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2000 Accepted Submission(s): 875
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.
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
Author
WhereIsHeroFrom
Source
HDOJ Monthly Contest – 2010.04.04
题意:给你一字符串,问最小字典序还有最大字典序的最小下标,以及包含最小字典序和最大字典序的串的开始的不同下标个数。
不知道动不动就TLE什么鬼……
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 6 using namespace std; 7 8 const int maxn = 1e6+7; 9 10 char s[maxn*2], str[maxn]; 11 int Next[maxn], len; 12 13 void Getnext(char s[]) 14 { 15 int j, k; 16 j = 0; 17 k = Next[0] = -1; 18 while(j < len) 19 { 20 while(k != -1 && s[j] != s[k]) 21 k = Next[k]; 22 Next[++j] = ++k; 23 } 24 } 25 26 int Getmax(char s[]) // 最大最小表示法 27 { 28 int i, j; 29 i = 0; 30 j = 1; 31 while(i < len && j < len) 32 { 33 int k = 0; 34 while(s[i+k] == s[j+k] && k < len) 35 k++; 36 if(k == len) 37 break; 38 39 if(s[i+k] > s[j+k]) 40 { 41 if(j+k > i) 42 j = j+k+1; 43 else 44 j = i + 1; 45 } 46 else 47 { 48 if(i+k > j) 49 i = i + k + 1; 50 else 51 i = j + 1; 52 } 53 } 54 return min(i, j); 55 } 56 57 int Getmin(char s[]) 58 { 59 int i, j; 60 i = 0; 61 j = 1; 62 while(i < len && j < len) 63 { 64 int k = 0; 65 while(s[i+k] == s[j+k] && k < len) 66 k++; 67 if(k == len) 68 break; 69 if(s[i+k] < s[j+k]) 70 { 71 if(j+k > i) 72 j = j+k+1; 73 else 74 j = i + 1; 75 } 76 else 77 { 78 if(i+k > j) 79 i = i + k + 1; 80 else 81 i = j + 1; 82 } 83 } 84 return min(i, j); 85 } 86 87 int main() 88 { 89 while(gets(str)) 90 { 91 len = strlen(str); 92 strcpy(s, str); 93 strcat(s, str); 94 95 memset(Next, 0, sizeof(Next)); 96 97 Getnext(str); 98 int t = 1, k = len - Next[len]; // k 最小循环节的长度,最小循环节,神奇的东西…… 99 100 if(len % k == 0) 101 t = len / k; 102 printf("%d %d %d %d\n", Getmin(s)+1, t, Getmax(s)+1, t); 103 } 104 return 0; 105 }
写代码你就好好写代码………………………………………………………………………………………………