hdu3374——最大最小表示法+循环节

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374

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

题目翻译:

给你一个长度为N的字符串,您可以通过左移生成 N 字符串。例如,‎
‎让我们考虑字符串"SKYLONG",我们可以生成七个字符串: 字符串排名‎
‎SKYLONG ‎
‎1 KYLONGs 2 ‎
‎YLONGSK ‎
‎3 龙基 4 ‎
‎ ‎
‎ONGSKYL 5 ‎
‎NGSKYLO 6 GSKYLON 7 ‎
‎而字典学第一是GSKYLON,字典上最后是YLONGSK,两者只出现过一次。‎
‎您的任务很简单,计算字典图形化的 fisrt 字符串的等级(如果有多个答案,请选择最小的答案),其时间,按字典顺序排列的最后一个字符串的等级(如果有多个答案,请选择最小的答案),并计算其时间也。‎

‎输入‎

‎每行包含一行字符串 S 的长度为 N (N <= 100000), 由小写字母组成。‎

‎输出‎

‎输出四个整数,由一个空格分隔,字典学词字符串的等级(如果有多个答案,请选择最小的答案),字符串在 N 生成的字符串中的时间,按字典顺序最后一个字符串的"排名"(如果有多个答案),选择最小的一个),它的时间也。

 

最大最小表示法裸题+循环节

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

string s;
int nxt[1000005];

void getnext(string str){
    int i = 0, j = -1, len = str.size();
    nxt[0] = -1;
    while(i < len){
        if(j == -1 || str[i] == str[j])
            nxt[++i] = ++j;
        else
            j = nxt[j];
    }
}
int posmin(int len){
    int i=0,j=1,k=0;
    while(i<len&&j<len&&k<len){
        int pan=s[(i+k)%len]-s[(j+k)%len];
        if(!pan)
            k++;
        else{
            if(pan>0)
                i+=k+1;
            else
                j+=k+1;
            if(i==j)
                j++;
            k=0;
        }
    }
    return min(i+1,j+1);
}

int posmax(int len){
    int i=0,j=1,k=0;
    while(i<len&&j<len&&k<len){
        int pan=s[(i+k)%len]-s[(j+k)%len];
        if(!pan)
            k++;
        else{
            if(pan>0)
                j+=k+1;
            else
                i+=k+1;
            if(i==j)
                j++;
            k=0;
        }
    }
    return min(i+1,j+1);
}

int main(){
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    while(cin>>s){
        int cnt = 1;
        int len = s.size();
        getnext(s);
        if(len%(len-nxt[len])==0)
            cnt = len/(len-nxt[len]);
        cout << posmin(len) << ' ' << cnt << ' ' << posmax(len) << ' ' << cnt << endl;
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值