• HDU-3374


    String Problem

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2057    Accepted Submission(s): 897


    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
    /**
        题意:给一个串,然后求串的最小表示法和最大表示法,并且输出有几个
        做法:串的最小表示法 + KMP
    **/
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <string.h>
    #include <stdio.h>
    #include <queue>
    #include <map>
    #define maxn 1000005
    using namespace std;
    int mmin, mmax;
    int len;
    char t1[maxn];
    char str[maxn << 1];
    char t2[maxn];
    void kmp_pre(char x[], int m, int nxt[])
    {
        int i, j;
        j = nxt[0] = -1;
        i = 0;
        while(i < m)
        {
            while(-1 != j && x[i] != x[j]) {
                j = nxt[j];
            }
            nxt[++i] = ++ j;
        }
    }
    int nxt[maxn];
    int kmp_count(char x[], int m, char y[], int n)
    {
        int i, j;
        int ans = 0 ;
        kmp_pre(x, m, nxt);
        i = j = 0;
        while(i < n)
        {
            while(-1 != j && y[i] != x[j]) {
                j = nxt[j];
            }
            i++;
            j++;
            while(j >= m)
            {
                ans ++;
                j = nxt[j];
            }
        }
        return ans;
    }
    void matchmin(char  s[])
    {
        int i, j, k, l;
        int N = len;
        for(i = 0, j = 1; j < N;)
        {
            for(k = 0; k < N && s[i + k] == s[j + k]; k++);
            if(k >= N) {
                break;
            }
            if(s[i + k] < s[j + k]) {
                j += k + 1;
            }
            else
            {
                l = i + k;
                i = j;
                j = max(l, j) + 1;
            }
        }
        mmin = i + 1;
    }
    void  matchmax(char s[])
    {
        int i, j, k, l;
        int N = len;
        for(i = 0, j = 1; j < N;)
        {
            for(k = 0; k < N && s[i + k] == s[j + k]; k++);
            if(k >= N) {
                break;
            }
            if(s[i + k] > s[j + k]) {
                j += k + 1;
            }
            else
            {
                l = i + k;
                i = j;
                j = max(l, j) + 1;
            }
        }
        mmax = i + 1;
    }
    int main()
    {
        while(~scanf("%s", str))
        {
            int n;
            string tmp;
            len = strlen(str);
            for(int i = 0; i < len; i++)
            {
                str[i + len] = str[i];
            }
            matchmin(str);
            matchmax(str);
            for(int i = 0; i < len; i++)
            {
                t1[i] = str[i + mmin - 1];
                t2[i] = str[i + mmax - 1];
            }
            int res1 = kmp_count(t1, len, str, len + len);
            int res2 = kmp_count(t2, len, str, len + len);
            if(mmin == 1) {
                res1 --;
            }
            if(mmax == 1) {
                res2 --;
            }
            printf("%d %d %d %d
    ", mmin, res1, mmax, res2);
        }
        return 0;
    }
  • 相关阅读:
    gdb 调试(查看运行时数据)(五)
    Kendo UI开发教程(23): 单页面应用(一)概述
    Kendo UI开发教程(24): 单页面应用(二) Router 类
    Kendo UI开发教程(26): 单页面应用(四) Layout
    Kendo UI开发教程(25): 单页面应用(三) View
    Kendo UI开发教程(27): 移动应用开发简介
    [置顶] Kendo UI开发教程: Kendo UI 示例及总结
    web端、android端的文件上传
    [置顶] 关于本博客 http://www.imobilebbs.com
    hdu4707 Pet
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4852586.html
Copyright © 2020-2023  润新知