HDU 3374 String Problem (字符串最小表示法+KMP)

String Problem

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


 

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

题意:

给你一个字符串(长度不超过1e6),字符串是一个环,让你求其中字典序最小和最大的,并求其出现的次数(在整个环中)。

思路:

字符串的最小表示法求出最小、最大字典序的串,然后kmp跑就行了。

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=2000010;
char str[maxn];
char s[maxn],p[maxn];
int len;
int n,m,nxt[maxn];
int cnt,as[maxn],ans;
void getfail()
{
    int i=0,j=-1;
    nxt[0]=-1;
    while(i0?p1+=k+1:p2+=k+1;
            if(p1==p2) p2++;
            k=0;
        }
    }
    return min(p1,p2);
}
int find_pos2()
{
    int p1=0,p2=1,k=0;
    while(p1

 

你可能感兴趣的:(字符串处理:字符串最小表示法,字符串处理:KMP)