zoukankan      html  css  js  c++  java
  • HDU 3374 String Problem

    String Problem

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


    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
     解题方法:先用最小表示法和最大表示法求出其开始位置,然后用KMP算法求出循环次数。
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    char str[1000005];  
    int _next[1000005];  
    
    int Getmin(char str[])
    {
        int nLen = strlen(str);
        int i = 0, j = 1, k;
        while(i < nLen && j < nLen)
        {
            for (k = 0; k < nLen; k++)
            {
                if (str[(i + k) % nLen] != str[(j + k) % nLen])
                {
                    break;
                }
            }
            if (str[(i + k) % nLen] > str[(j + k) % nLen])
            {
                i += k + 1;
            }
            else
            {
                j += k + 1;
            }
            if (i == j)
            {
                j++;
            }
        }
        return min(i, j);
    }
    
    int Getmax(char str[])
    {
        int nLen = strlen(str);
        int i = 0, j = 1, k;
        while(i < nLen && j < nLen)
        {
            for (k = 0; k < nLen; k++)
            {
                if (str[(i + k) % nLen] != str[(j + k) % nLen])
                {
                    break;
                }
            }
            if (str[(i + k) % nLen] < str[(j + k) % nLen])
            {
                i += k + 1;
            }
            else
            {
                j += k + 1;
            }
            if (j ==i)
            {
                j++;
            }
        }
        return min(i, j);
    }
    
    void Getnext(char str[])
    {
        int j = 0, k = -1;
        int nLen = strlen(str);
        _next[0] = -1;
        while(j < nLen)
        {
            if (k == -1 || str[k] == str[j])
            {
                j++;
                k++;
                _next[j] = k;
            }
            else
            {
                k = _next[k];
            }
        }
    }
    
    int main()
    {
        while(scanf("%s", str) != EOF)
        {
            int n = Getmin(str);
            int n1 = Getmax(str);
            Getnext(str);
            int nLen = strlen(str);
            int nCount = 0;
            if (nLen % (nLen - _next[nLen]) == 0)
            {
                nCount = nLen / (nLen - _next[nLen]);
            }
            else
            {
                nCount = 1;
            }
            printf("%d %d %d %d
    ", n + 1, nCount, n1 + 1, nCount);
        }
        return 0;
    }
    Author
    WhereIsHeroFrom
     
  • 相关阅读:
    Linux之安装python3.6.6
    Python之计算器
    springboot项目快速代码生成工具
    电脑忘记密码怎么办?
    HTML编辑器
    WCF的几种寄宿方式
    获取客户端IP 和 外网IP
    发送短信验证码
    动态库,服务tips
    asp.net WebService 与 WCF的区别
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3162833.html
Copyright © 2011-2022 走看看