现在的位置: 首页 > 综合 > 正文

hdu3374—String Problem

2019年02月14日 ⁄ 综合 ⁄ 共 2896字 ⁄ 字号 评论关闭

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;
}

【上篇】
【下篇】

抱歉!评论已关闭.