标签:
#include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> using namespace std; const int MAXN = 1e6+7; char s[MAXN<<1], s1[MAXN]; int Next[MAXN]; void GetNext(char s[], int N) { int i=0, j=-1; Next[0] = -1; while(i < N) { if(j==-1 || s[i]==s[j]) Next[++i] = ++j; else j = Next[j]; } } int GetMin(char s[], int N) {///求最小的字典序的开始 int i=0, j=1; while(i<N && j<N) { int k = 0; while(s[i+k] == s[j+k] && k<N) k++; if(k == N)break; if(s[i+k] < s[j+k]) { if(j+k > i) j = j+k+1; else j = i+1; } else { if(i+k > j) i = i+k+1; else i = j+1; } } return min(i, j); } int GetMax(char s[], int N) {///求最大的字典序的开始 int i=0, j=1; while(i<N && j<N) { int k = 0; while(s[i+k] == s[j+k]) k++; if(k == N)break; if(s[i+k] > s[j+k]) { if(j+k > i) j = j+k+1; else j = i+1; } else { if(i+k > j) i = i+k+1; else i = j+1; } } return min(i, j); } int main() { while(scanf("%s", s1) != EOF) { int N = strlen(s1); strcpy(s, s1); strcat(s, s1); GetNext(s, N); int circle = N-Next[N], times=1; if(N % circle == 0) times = N / circle; printf("%d %d %d %d\n", GetMin(s, N)+1, times, GetMax(s, N)+1, times); } return 0; }
String Problem - HDU 3374 (kmp+最大最小表示)
标签:
原文地址:http://www.cnblogs.com/liuxin13/p/4742391.html