【HDU3374】 String Problem (最小最大表示法+KMP)

本文介绍了一种通过左移操作生成字符串的所有循环排列,并找出字典序最小和最大的排列及其出现次数的方法。利用KMP算法优化查找过程,通过比较字符来确定最小和最大表示形式。

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

String 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求个数。
  注意特殊的时候在KMP中加入长度特判。

 

  最小最大表示法是求出循环子串得到最小最大表示。

  表示我之前并没有遇到过,貌似还有些神奇的应用?不知道、、

  但是打起来还是挺简单的,也很容易证明。

 

  

int get_st(bool p)
{
	int i=1,j=2,k=0;
	while(i<=l&&j<=l&&k<=l)
	{
		if(s[i+k]==s[j+k]) k++;
		else if((s[i+k]>s[j+k])==(!p))
		{
			i+=k+1;
			k=0;
		}
		else
		{
			j+=k+1;
			k=0;
		}
		if(i==j) j++;
	}
	return mymin(i,j);
}

  

  这个我把最小表示和最大表示合在一起了。p=0是最小,p=1时最大。

  证明的话...连续匹配过程中一旦发现字符有小于其他字符的,那么他一定不是最大表示,且他前面连续的一段也不是。

 

本题代码如下:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 using namespace std;
 8 #define Maxn 1000010
 9 
10 char s[2*Maxn];
11 int nt[Maxn*2],l;
12 
13 int mymin(int x,int y) {return x<y?x:y;}
14 
15 int get_st(bool p)
16 {
17     int i=1,j=2,k=0;
18     while(i<=l&&j<=l&&k<=l)
19     {
20         if(s[i+k]==s[j+k]) k++;
21         else if((s[i+k]>s[j+k])==(!p))
22         {
23             i+=k+1;
24             k=0;
25         }
26         else
27         {
28             j+=k+1;
29             k=0;
30         }
31         if(i==j) j++;
32     }
33     return mymin(i,j);
34 }
35 
36 void get_nt(int x)
37 {
38     nt[x]=x-1;int p=x-1;
39     for(int i=x+1;i<=x+l-1;i++)
40     {
41         while(s[i]!=s[p+1]&&p>=x) p=nt[p];
42         if(s[i]==s[p+1]) p++;
43         nt[i]=p;
44     }
45 }
46 
47 int ffind(int x)
48 {
49     int ans=0;int p=x-1;
50     for(int i=1;i<=2*l;i++)
51     {
52         while((s[i]!=s[p+1]&&p>=x)||p-x+1>=l) p=nt[p];
53         if(s[i]==s[p+1]&&p-x+1<l) p++;
54         if(i>l&&p-x+1>=l) ans++;
55     }
56     return ans;
57 }
58 
59 int main()
60 {
61     while(scanf("%s",s+1)!=EOF)
62     {
63         l=strlen(s+1);
64         for(int i=1;i<=l;i++) s[i+l]=s[i];
65         int st=get_st(0);
66         get_nt(st);
67         printf("%d %d ",st,ffind(st));
68         
69         st=get_st(1);
70         get_nt(st);
71         printf("%d %d\n",st,ffind(st));
72     }
73     return 0;
74 }
HDU3374

 

2016-08-17 15:59:59

 

转载于:https://www.cnblogs.com/Konjakmoyu/p/5780363.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值