hdu3374(最小表示法+KMP)

String Problem

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


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
 

Recommend
lcy
 
本题要求一个给定字符串的最小、最大表示的起始下标和其对应的出现次数。
求最小、最大表示的起始下标可以直接改动最小表示法的实现算法;而其对应的出现次数可以先求得最小、最大表示的字符串,然后运用KMP算法找循环节。
最小表示法+KMP
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

const int MAXN=1000000+100;
int next[MAXN];

//KMP算法中计算next[]数组
void getNext(char *p)
{
    int j,k,len=strlen(p);
    j=0;
    k=-1;
    next[0]=-1;
    while(j<len)
    {
        if(k==-1||p[j]==p[k])
        {
            next[++j]=++k;
        }
        else k=next[k];
    }
}

//返回母串的最小子串的起始位置
int minpresent(char *str)
{
	int i,j,k,len=strlen(str);
	i=0,j=1,k=0;
	while(i<len&&j<len&&k<len)
	{
		if(str[(i+k)%len]==str[(j+k)%len])
			k++;
		else 
		{
			if(str[(i+k)%len]>str[(j+k)%len])
			i=i+k+1;
		    else 
			j=j+k+1;
			if(i==j)j++;
			k=0;
		}
	}
	return ++i<++j?i:j;
}

//返回母串的最大子串的起始位置
int maxpresent(char *str)
{
	int i,j,k,len=strlen(str);
	i=0,j=1,k=0;
	while(i<len&&j<len&&k<len)
	{
		if(str[(i+k)%len]==str[(j+k)%len])
			k++;
		else 
		{
			if(str[(i+k)%len]<str[(j+k)%len])
			i=i+k+1;
		    else 
			j=j+k+1;
			if(i==j)j++;
			k=0;
		}
	}
	return ++i<++j?i:j;
}

char str[MAXN];
char Minstr[MAXN];
char Maxstr[MAXN];

int main()
{
	int n,i,Minid,Minlen,Minans,Maxid,Maxlen,Maxans;
	while(~scanf("%s",str))
	{
		Minstr[0]=0;
		Minid=minpresent(str)-1;
		strcpy(Minstr,str+Minid);
		strncat(Minstr,str,Minid);
	//	printf("Minstr=%s\n",Minstr);
		getNext(Minstr);
		Minlen=strlen(Minstr);
		Minans=1;
		if(Minlen%(Minlen-next[Minlen])==0)
			Minans=Minlen/(Minlen-next[Minlen]);
		//printf("Minid=%d   Minans=%d\n",Minid+1,Minans);

		Maxstr[0]=0;
		Maxid=maxpresent(str)-1;
		strcpy(Maxstr,str+Maxid);
		strncat(Maxstr,str,Maxid);
	//	printf("Maxstr=%s\n",Maxstr);
		getNext(Maxstr);
		Maxlen=strlen(Maxstr);
		Maxans=1;
		if(Maxlen%(Maxlen-next[Maxlen])==0)
			Maxans=Maxlen/(Maxlen-next[Maxlen]);
	//	printf("Maxid=%d   Maxans=%d\n",Maxid+1,Maxans);

		printf("%d %d %d %d\n",Minid+1,Minans,Maxid+1,Maxans);
	}
	return 0;
}

你可能感兴趣的:(数据结构,字符串,KMP,最小表示法)