hdu3374 String Problem

 地址: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
",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