hdu3374---String Problem

46 篇文章 0 订阅

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
HDOJ Monthly Contest – 2010.04.04

Recommend
lcy | We have carefully selected several similar problems for you: 3371 3372 3373 3375 3376

串的最大(小)表示法+kmp

/*************************************************************************
    > File Name: hdu3374.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年02月17日 星期二 19时44分51秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 1000010;
int next[N];
char S[N << 1];
char T[N];

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

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

void get_next ()
{
    next[0] = -1;
    int j = 0;
    int k = -1;
    int len = strlen (T);
    while (j < len)
    {
        if (k == -1 || T[j] == T[k])    
        {
            next[++j] = ++k;
        }
        else
        {
            k = next[k];
        }
    }
}

int KMP ()
{
    int lens = strlen (S);
    int lent = strlen (T);
    int i = 0;
    int j = 0;
    int ret = 0;
    while (i < lens && j < lent)
    {
        if (j == -1 || S[i] == T[j])
        {
            ++i;
            ++j;
            if (j == lent)
            {
                j = next[j];
                ++ret;
            }
        }
        else
        {
            j = next[j];
        }
    }
    return ret;
}

int main ()
{
    while (~scanf("%s", S))
    {
        int x1 = minway();
        int x2 = maxway();
        int len = strlen (S);
        for (int i = len; i < 2 * len - 1; ++i)
        {
            S[i] = S[i - len];
        }
        for (int i = x1; i < x1 + len; ++i)
        {
            T[i - x1] = S[i]; 
        }
        S[2 * len - 1] = '\0';
        T[len] = '\0';
        get_next();
        printf("%d %d ", x1 + 1, KMP());
        for (int i = x2; i < x2 + len; ++i)
        {
            T[i - x2] = S[i];
        }
        get_next();
        printf("%d %d\n", x2 + 1, KMP());
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值