next数组 + 最小最大表示

本文探讨了如何通过分析字符串的循环特性,找到其字典序最小和最大的表示形式,利用next数组确定最短循环节,从而计算出特定字符串在左移生成的所有字符串中的排名及其出现次数。

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

给出一个串,求出它最小最大表示开始的位置以及会出现的长度

这个长度,其实是用next数组求出的,这个得需要找规律吧,要多想一下,next数组求最短循环节。

/*
模板
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int MAX=1000000+10;
char str[MAX];
int Next[MAX];
/*
比找最小表示法,相当于找着一个字符串a,再找一个一直循环比较
如果他是最小的,就会一直增加比较,直到长度为len则返回
如果出现i和j相等的情况,j加1最后返回小的那一个就行
*/
void Get_Next(int len)
{
    int i=0,j=-1;
    Next[0]=-1;
    while(str[i])
    {
        if(j==-1||str[i]==str[j])
        {
            i++;
            j++;
            Next[i]=j;
        }
        else
            j=Next[j];
    }
}
int min_max_express(char *s,int len,bool flag) //最小表示法和最大表示法写到一块儿了
{
    int i=0,j=1,k=0;
    while(i<len&&j<len&&k<len) //一次就能够找着
    {
        int t=s[(j+k)%len]-s[(i+k)%len];//比较两个字符
        if(t==0)
            k++;//相等,就后移比较下一个字符
        else
        {
            if(flag) //最小表示法
            {
                if(t>0)
                    j+=k+1;//+(k+1)相当于直接把相等的跳过
                else
                    i+=k+1;
                //cout<<i<<" sdf"<<j<<"df"<<k<<endl;
            }
            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",str)!=EOF)
    {
        int len=strlen(str);
        int min_express=min_max_express(str,len,true);
        int max_express=min_max_express(str,len,false);
        Get_Next(len);
        int ans=len%(len-Next[len])?1:len/(len-Next[len]);
        printf("%d %d %d %d\n",min_express+1,ans,max_express+1,ans);//因为从0开始的,最大最小项+1
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值