hdu3374 String Problem

字符串旋转排名算法
本文介绍了一个算法问题,即计算一个给定字符串通过左移产生的所有可能字符串中字典序最前与最后的字符串及其出现次数。文章提供了完整的C++代码实现,并采用KMP算法进行子串匹配。

 地址:http://acm.hdu.edu.cn/showproblem.php?pid=3374

题目:

String Problem

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


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
 

 

Recommend
lcy
 
思路:最大最小表示法
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=2e6+7;
12 const int mod=1e9+7;
13 
14 int nt[K];
15 char sa[K];
16 void kmp_next(char *T,int *next)
17 {
18     next[0]=0;
19     for(int i=1,j=0,len=strlen(T);i<len;i++)
20     {
21         while(j&&T[i]!=T[j]) j=next[j-1];
22         if(T[i]==T[j])  j++;
23         next[i]=j;
24     }
25 }
26 int kmp(char *S,char *T,int *next)
27 {
28     int ans=0;
29     int ls=strlen(S),lt=strlen(T);
30     kmp_next(T,next);
31     for(int i=0,j=0;i<ls;i++)
32     {
33         while(j&&S[i]!=T[j]) j=next[j-1];
34         if(S[i]==T[j])  j++;
35         if(j==lt)   ans++;
36     }
37     return ans;
38 }
39 //ff为真表示最小,为假表示最大
40 int mx_mi_express(char *S,bool ff,int len)
41 {
42     int i=0,j=1,k;
43     while(i<len&&j<len)
44     {
45         k=0;
46         while(k<len&&S[i+k]==S[j+k]) k++;
47         if(k==len)  return i<=j?i:j;
48         if((ff&&S[i+k]>S[j+k]) || (!ff&&S[i+k]<S[j+k]))
49         {
50             if(i+k+1>j) i=i+k+1;
51             else    i=j+1;
52         }
53         else if((ff&&S[i+k]<S[j+k]) || (!ff&&S[i+k]>S[j+k]))
54         {
55             if(j+k+1>i) j=j+k+1;
56             else    j=i+1;
57         }
58     }
59     return i<=j?i:j;
60 }
61 int main(void)
62 {
63     int t,n;cin>>t;
64     while(scanf("%s",sa)==1)
65     {
66         kmp_next(sa,nt);
67         int len=strlen(sa);
68         int num=len-nt[len-1];
69         if(num!=len&&len%num==0)num=len/num;
70         else    num=1;
71         for(int i=0;i<len;i++)
72             sa[i+len]=sa[i];
73         printf("%d %d %d %d\n",1+mx_mi_express(sa,1,len),num,1+mx_mi_express(sa,0,len),num);
74     }
75     return 0;
76 }

 

 

转载于:https://www.cnblogs.com/weeping/p/6669865.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值