HDU 3374 String 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


【题目大意】
给定一些环,如果按照顺时针的方式破环为链,那么最大的字符串,最小的字符串出现的位置(最小的)以及出现的次数。


【题目分析】
    字符串的最大/最小表示法。因为最大最小状态出现的次数是比较好解决的,只需要求出完整的循环节的个数就可以了。而最大最小表示法的方法比较巧妙,建议读者查找相关资料自行解决,这里不再赘述。方法的大意就是排除所有不可能是结果的情况,那么剩下的就是最终的答案了。
   


【代码】
(略丑,因为把最大最小表示法写到了一起,比较难看,但是十分简短)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char a[1000001];
int ne[1000001];int l,len,ans,minn,maxx;
inline int query(int flag)
{
    int i=0,j=1,k=0;
    while (i<=l&&j<=l&&k<=l)
    {
        int t=a[(i+k)%l+1]-a[(j+k)%l+1];
        if (t==0) k++;
        else
        {
            if (flag){
                if (t>0) j+=k+1;
                else i+=k+1;
            }
            else {
                if (t>0) i+=k+1;
                else j+=k+1;
            }
            if (i==j) j++;
            k=0;
        }
    }
    return min(i,j);
}
int main() 
{
    while (scanf("%s",a+1)!=EOF)
    {
        l=strlen(a+1);
        for (int i=2,j=0;i<=l;++i)
        {
            while (j&&a[j+1]!=a[i]) j=ne[j];
            if (a[j+1]==a[i]) j++;
            ne[i]=j;
        }
        int len=l-ne[l];
        int ans=l%len?1:l/len;
        int minn=query(1),maxx=query(0);
        printf("%d %d %d %d\n",maxx+1,ans,minn+1,ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值