kmp+最大最小表示
http://acm.hdu.edu.cn/showproblem.php?pid=3374
String Problem
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的Next数组得到。这个和yobobobo曾经研究过半天。。
对于最大最小表示法,就是将字符串不断旋转,得到字典序最大或者最小的。
求字符串最小表示的方法
(1) 利用两个指针p1, p2。初始化时p1指向s[0], p2指向s[1]。
(2) k = 0开始,检验s[p1+k] 与 s[p2+k] 对应的字符是否相等,如果相等则k++,一直下去,直到找到第一个不同,(若k试了一个字符串的长度也没找到不同,则那个位置就是最小表示位置,算法终止并返回)。则该过程中,s[p1+k] 与 s[p2+k]的大小关系,有三种情况:
(A). s[p1+k] > s[p2+k],则p1滑动到p1+k+1处 --- 即s1[p1->p1+k]不会
是该循环字符串的“最小表示”的前缀。 k置为0
(B). s[p1+k] < s[p2+k],则p2滑动到p2+k+1处, k置为0
(C). s[p1+k] = s[p2+k],则 k++; if (k == len) 返回结果。
注:这里滑动方式有个小细节,若滑动后p1 == p2,将正在变化的那个指针再+1。直到p1、p2把整个字符串都检验完毕,返回两者中小于 len 的值。
(3) 如果 k == len, 则返回p1与p2中的最小值
其实最大表示法一样,大小于的时候改变一下就照了,可以写在一个函数里面。
AC代码:
#include <iostream>
using namespace std;
#define NSIZ 1000100
#define MSIZ 2000100
char str[NSIZ];
char tmpstr[MSIZ];
int Next[NSIZ];
int minPose(char str[], int n)
{
int p1 = 0, p2 = 1, k = 0;
int cmp = 0;
while(p1 < n && p2 <n && k < n)
{
cmp = str[(p1 + k) % n] - str[(p2 + k)%n]; //关键代码,通过这个值和0的关系来求最大最小的位置
if (!cmp)
{
++k;
}
else
{
if (cmp > 0)
{
p1 += k +1;
}
else
{
p2 += k + 1;
}
p2 += (p1==p2);
k = 0;
}
}
return min(p1, p2);
}
int maxPose(char str[], int n)
{
int p1 = 0, p2 = 1, k = 0;
int cmp = 0;
while(p1 < n && p2 <n && k < n)
{
cmp = str[(p1 + k)%n] - str[(p2 + k)%n]; //关键代码,通过这个值和0的关系来求最大最小的位置
if (!cmp) //这个最大最小表示的区别就是就是把大于零小于零的标记位置的i和j
//换一下
{
++k;
}
else
{
if (cmp < 0)
{
p1 += k +1;
}
else
{
p2 += k + 1;
}
k = 0;
p2 += (p1==p2);
}
}
return min(p1, p2);
}
void getNext(char str[], int n)
{
int i = 0, j = -1;
Next[i] = -1;
while(i < n)
{
if (-1 == j || str[i] == str[j])
{
++i; ++j;
Next[i] = j;
}
else
{
j = Next[j];
}
}
}
int kmp(char str1[], char * str2)
{
int i = -1, j = -1;
int n1 = strlen(str1);
int n2 = strlen(str2);
int res = 0;
getNext(str2, n2);
while(i < n1)
{
if(-1 == j || str1[i] == str2[j])
{
++i;
++j;
}
else
{
j = Next[j];
}
if (j == n2)
{
++res;
}
}
return res;
}
int main()
{
int i = 0;
int minRank, minTime, maxRank, maxTime;
while(scanf("%s", str) != EOF)
{
int n = strlen(str);
strcpy(tmpstr, str);
strcat(tmpstr, str);
tmpstr[2 * n -1] = 0;
minRank = minPose(str, n);
maxRank = maxPose(str, n);
strncpy(str, tmpstr + minRank, n);
str[n] = 0;
minTime = maxTime = kmp(tmpstr, str);
printf("%d %d %d %d\n", minRank + 1 , minTime, maxRank+1, maxTime);
}
return 0;
}