ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

hdu3374最大最小表示法加kmp

2019-03-02 16:49:48  阅读:181  来源: 互联网

标签:string int hdu3374 ans Rank 表示法 choose kmp lexicographically


 

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
​
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1000005
using namespace std;
char p[maxn];
int m;
int f[maxn];
void getfail(char *P, int *f)
{

    f[0] = f[1] = 0;
    for(int i = 1; i < m; i++)//虽然字符串是0到m-1,但是要求出f[m]的值
    {
        int j = f[i];
        while(j && P[i] != P[j]) j = f[j];
        f[i + 1] = P[i] == P[j] ? j + 1 : 0;
    }
}
int getmin(char*s)
{


    int i=0,j=1,k=0;
    while(i<m&&j<m&&k<m)
    {
        int t=s[(i+k)%m]-s[(j+k)%m];
        if(t==0)
            k++;
        else
        {
            if(t>0)
                i+=k+1;
            else
                j+=k+1;
            if(i==j)
                j++;
            k=0;
        }
    }
    return min(i,j);
}
int getmax(char*s)
{
    int i=0,j=1,k=0;
    while(i<m&&j<m&&k<m)
    {
        int t=s[(i+k)%m]-s[(j+k)%m];
        if(t==0)
            k++;
        else
        {
            if((t>0))
                j+=k+1;
            else
                i+=k+1;
            if(i==j)
                j++;
            k=0;
        }
    }
    return min(i,j);
}
int main()
{
    while(~scanf("%s",p))
    {
        m=strlen(p);
        getfail(p,f);
        int ans;
        if(m%(m-f[m])==0)
            ans=m/(m-f[m]);
            else ans=1;
            printf("%d %d %d %d\n",getmin(p)+1,ans,getmax(p)+1,ans);
    }
    return 0;
}

​

 

标签:string,int,hdu3374,ans,Rank,表示法,choose,kmp,lexicographically
来源: https://blog.csdn.net/sdauguanweihong/article/details/87989560

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有