hdu3374String Problem(kmp+最小表示+next數組理解)

String Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1382    Accepted Submission(s): 618


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
 

kmp+最小表示法
至於出現次數,其實就是看看存不存在字串相加等於原串,最大和最小兩次的time是一樣的。

int getMin(char *str, int l){ //l爲字符串的長度
	int i=0, j=1, k=0;
	while(i<l && j<l && k<l){
		int t = str[(i+k)%l]-str[(j+k)%l];
		if(!t) k++;
		else{
			if(t>0) i=i+k+1;
			else j=j+k+1;
			if(j==i) j++;
			k=0;
		}
	}
	return min(i,j);
}

int getMax(char *str, int l){
	int i=0, j=1, k=0;
	while(i<l && j<l && k<l){
		int t = str[(i+k)%l]-str[(j+k)%l];
		if(!t) k++;
		else{
			if(t>0) j=j+k+1;  //求最大最小隻有這不一樣
			else i=i+k+1;
			if(i==j) j++;
			k=0;
		}
	}
	return min(i,j);
}




#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1000005;
char word[MAXN];
int next[MAXN];

int getMin(char *str, int len){
    int i=0, j=1, k=0;
    while(i<len && j<len && k<len){
        int t = str[(i+k)%len]-str[(j+k)%len];
        if(!t) k++;
        else{
            if(t>0) i=i+k+1;
            else j=j+k+1;
            if(j==i) j++;
            k=0;
        }
    }
    return min(i,j);
}

int getMax(char *str, int len){
    int i=0,j=1,k=0;
    while(i<len && k<len && j<len){
        int t = str[(i+k)%len]-str[(j+k)%len];
        if(!t) k++;
        else{
            if(t>0) j=j+k+1;
            else i=i+k+1;
            if(i==j) j++;
            k=0;
        }
    }
    return min(i,j);
}

void get_next(char *str, int len){
    int i=0,j=-1;
    next[i]=j;
    while(i<len){
        if(j==-1 || str[i]==str[j]){
            i++; j++;
            next[i]=j;
        }else j=next[j];
    }
}

int main()
{
    while(cin>>word){
        int len = strlen(word);
        int minn = getMin(word,len);
        int maxn = getMax(word,len);
        get_next(word,len);
        int cir = len-next[len];
        int time = 1;
        if(len%cir==0) time = len/cir;
        cout<<minn+1<<" "<<time<<" "<<maxn+1<<" "<<time<<endl;
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章