hdu 3374 String Problem

Posted 日拱一卒 功不唐捐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 3374 String Problem相关的知识,希望对你有一定的参考价值。

String Problem

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

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


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   |   We have carefully selected several similar problems for you:  3371 3372 3373 3375 3376 
 
题意:
在字符串所有的循环字串中,输出字典序最小的序号、出现次数、字典序最大的序号、出现次数
多个位置输出最小的序号
 
字典序最小、最大用字符串的最小表示法
出现次数就是循环节用kmp(最小、最大的出现次数相同)
 
最小表示法求字典序最小:
1、两个指针i,j,开始i指向s[0],j指向s[1]
2、k=0,比较s[i+k]和s[j+k],直至两个不相等
     比较过程中,有3种情况:
    ① s[i+k] > s[j+k]  ,则i=i+k+1,即 i 到 i+k 不会作为字典序最小字符串的首字母出现
        因为选 j到j+k 更优,
    ② s[i+k] < s[j+k] ,则 j=j+k+1,原理同上
    ③ s[i+k] = s[j+k],k++,当k=len时,停止比较
3、i 和 j 中较小的那个就是 字典序最小的字符串的起始位置
 
优化:(不懂。。。。。。)
如果i+k+1 < j , i=j+1 
 
字典序最大 把上面的< > 交换即可
 
#include<cstdio>
#include<cstring>
#define N 1000001
using namespace std;
char s[N];
int len,cnt,minn,maxn;
int f[N];
int getmin()
{
    int i=0,j=1,k;
    while(i<len&&j<len)
    {
        k=0;
        while(k<len && s[(i+k)%len]==s[(j+k)%len]) k++;
        if(k==len) break;
        if(s[(i+k)%len]>s[(j+k)%len]) 
            if(i+k+1>j) i=i+k+1;
            else i=j+1;
        else if(j+k+1>i) j=j+k+1;
        else j=i+1;
    }
    return i<j ? i : j;
}
int getmax()
{
    int i=0,j=1,k;
    while(i<len&&j<len)
    {
        k=0;
        while(k<len && s[(i+k)%len]==s[(j+k)%len]) k++;
        if(k==len) break;
        if(s[(i+k)%len]<s[(j+k)%len]) 
            if(i+k+1>j) i=i+k+1;
            else i=j+1;
        else if(j+k+1>i) j=j+k+1;
        else j=i+1;
    }
    return i<j ? i : j;
}
void getfail()
{
    int j;
    for(int i=1;i<len;i++)
    {
        j=f[i];
        while(j && s[j]!=s[i]) j=f[j];
        f[i+1]= s[j]==s[i] ? j+1 : 0;
    }
}
int main()
{
    while(scanf("%s",s)!=EOF)
    {
        len=strlen(s);
        minn=getmin();
        maxn=getmax();
        getfail();
        cnt=len-f[len];
        if(len%cnt) cnt=1;
        else cnt=len/cnt;
        printf("%d %d %d %d\n",minn+1,cnt,maxn+1,cnt); 
    }
}

 

 

以上是关于hdu 3374 String Problem的主要内容,如果未能解决你的问题,请参考以下文章

hdu3374 String Problem最小表示法exKMP

hdu P3374 String Problem

hdu3374 String Problem(最小值表示法 + KMP)

hdu3374 String Problem 最小最大表示法 最小循环节出现次数

HDU 3374 String Proble

HDOJ3374 String Problem KMP+最小表示法