kuangbin专题十六 KMP&&扩展KMP HDU3347 String Problem(最小最大表示法+kmp)

本文深入探讨了一种用于计算字符串循环左移后的字典序最小及最大值排名的算法,通过构造Next数组来确定循环节,从而高效解决字符串排名问题。文章提供了完整的C++代码实现,包括预处理KMP表、获取最小和最大排名的函数,以及主函数中的输入输出处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.OutputOutput 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


只知道最小最大表示法。但是名次没思路。 后来看题解是循环节。。。。
脑子真是废了。。 循环节用next数组搞定 即可

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=2000010;
 6 char s[maxn],t[maxn];
 7 int Next[maxn],len;
 8 
 9 void prekmp(char* s) {
10     int i,j;
11     j=Next[0]=-1;
12     i=0;
13     while(i<len) {
14         while(j!=-1&&s[i]!=s[j]) j=Next[j];
15         if(s[++i]==s[++j]) Next[i]=Next[j];
16         else Next[i]=j;
17     }
18 }
19 
20 int getmin(char* str) {
21     int len2=strlen(str);
22     int i=0,j=1,k=0;
23     while(i<len&&j<len&&k<len) {
24         int tmp=s[(i+k)%len2]-s[(j+k)%len2];
25         if(!tmp) k++;
26         else {
27             if(tmp<0) j+=k+1;
28             else i+=k+1;
29             if(i==j) j++;
30             k=0;
31         }
32     }
33     return min(i,j);
34 }
35 
36 int getmax(char* str) {
37     int len2=strlen(str);
38     int i=0,j=1,k=0;
39     while(i<len&&j<len&&k<len) {
40         int tmp=s[(i+k)%len2]-s[(j+k)%len2];
41         if(!tmp) k++;
42         else {
43             if(tmp<0) i+=k+1;
44             else j+=k+1;
45             if(i==j) j++;
46             k=0;
47         }
48     }
49     return min(i,j);
50 }
51 
52 int main() {
53     while(~scanf("%s",s)) {
54         len=strlen(s);
55         prekmp(s);
56         int num=1,len1=len-Next[len];
57         if(len%len1==0)
58             num=len/len1;
59         strcpy(t,s);
60         strcat(s,t);
61         printf("%d %d %d %d\n",getmin(s)+1,num,getmax(s)+1,num);
62     }
63 }

 



转载于:https://www.cnblogs.com/ACMerszl/p/10321817.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值