kuangbin专题十六 KMP&&扩展KMP HDU3347 String Probl
发布时间:2020-12-14 05:13:51 所属栏目:大数据 来源:网络整理
导读: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
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,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,the string’s times in the N generated strings,and its times also.Sample Input abcder aaaaaa abababSample Output 1 1 6 1 1 6 1 6 1 3 2 3 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,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 %dn",getmin(s)+1,num,getmax(s)+1,num); 62 } 63 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |