zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    rabbitmq 学习10channel 说明
    ASP.NET中进行消息处理(MSMQ) 一
    ThreadLocal原理及用法详解
    设计模式责任链模式
    枚举详解之EnumSet、EnumMap用法
    设计模式策略模式和工厂模式结合使用
    Java并发编程系列之CyclicBarrier详解
    [解题报告]求平均成绩
    [解题报告]1005 Number Sequence
    [解题报告]1013Digital Roots
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4852586.html
Copyright © 2011-2022 走看看