kuangbin专题十六 KMP&&扩展KMP HDU3347 String Problem(最小最大表示法+kmp)

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.OutputOutput 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数组搞定 即可

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=2000010;
 6 char s[maxn],t[maxn];
 7 int Next[maxn],len;
 8 
 9 void prekmp(char* s) {
10     int i,j;
11     j=Next[0]=-1;
12     i=0;
13     while(i<len) {
14         while(j!=-1&&s[i]!=s[j]) j=Next[j];
15         if(s[++i]==s[++j]) Next[i]=Next[j];
16         else Next[i]=j;
17     }
18 }
19 
20 int getmin(char* str) {
21     int len2=strlen(str);
22     int i=0,j=1,k=0;
23     while(i<len&&j<len&&k<len) {
24         int tmp=s[(i+k)%len2]-s[(j+k)%len2];
25         if(!tmp) k++;
26         else {
27             if(tmp<0) j+=k+1;
28             else i+=k+1;
29             if(i==j) j++;
30             k=0;
31         }
32     }
33     return min(i,j);
34 }
35 
36 int getmax(char* str) {
37     int len2=strlen(str);
38     int i=0,j=1,k=0;
39     while(i<len&&j<len&&k<len) {
40         int tmp=s[(i+k)%len2]-s[(j+k)%len2];
41         if(!tmp) k++;
42         else {
43             if(tmp<0) i+=k+1;
44             else j+=k+1;
45             if(i==j) j++;
46             k=0;
47         }
48     }
49     return min(i,j);
50 }
51 
52 int main() {
53     while(~scanf("%s",s)) {
54         len=strlen(s);
55         prekmp(s);
56         int num=1,len1=len-Next[len];
57         if(len%len1==0)
58             num=len/len1;
59         strcpy(t,s);
60         strcat(s,t);
61         printf("%d %d %d %d\n",getmin(s)+1,num,getmax(s)+1,num);
62     }
63 }

 



posted @ 2019-01-25 21:47  Frontierone  阅读(125)  评论(0编辑  收藏  举报