zoukankan      html  css  js  c++  java
  • (KMP 最大表示最小表示)String Problem -- hdu-- 3374

    http://acm.hdu.edu.cn/showproblem.php?pid=3374

    String Problem

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


    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
     
    输出:最小字典序的编号,最小字典序个数,最大字典序编号,最大字典序个数。

    刚开始一直没明白是怎么回事,终于明白了吧,用暴力写了,明明知道时间超限,但我还是交了一次,果断的TLE,后来搜了搜,在看了许久后终于明白是怎么一回事,原来是用了两个指针, 用两个指针来动态比较,然后的然后就能求出最小字典串和最大字典串了,另外个数的问题其实只需要FindNext就可以求出来了,这个刚开始的时候是一直没想到,我只是想暴力求出来,然后发现自己忽略了这么好用的。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    
    using namespace std;
    
    const int maxn = 2000007;
    
    int Next[maxn], sum;
    char s1[maxn], s[maxn];
    
    void FindNext(char S[])
    {
        int i=0, j=-1;
        Next[0] = -1;
    
        int Slen = strlen(S);
    
        while(i<Slen)
        {
            if(j==-1 || S[i]==S[j])
                Next[++i] = ++j;
            else j = Next[j];
        }
    }
    
    int GetMin(char s[], int N)
    {
        int i=0, j=1;
    
        while(i<N && j<N)
        {
            int k=0;
            while(s[i+k]==s[j+k] && k<N) k++;
            if(k==N)
                break;
    
            if(s[i+k]<s[j+k])
            {
                if(j+k>i)
                    j += k+1;
                else
                    j = i+1;
            }
            else
            {
                if(i+k>j)
                    i += k+1;
                else
                    i = j+1;
            }
        }
    
        return min(i, j);
    }
    
    int GetMax(char s[], int N)
    {
        int i=0, j=1;
    
        while(i<N && j<N)
        {
            int k=0;
            while(s[i+k]==s[j+k] && k<N) k++;
            if(k==N)
                break;
    
            if(s[i+k]>s[j+k])
            {
                if(j+k>i)
                    j += k+1;
                else
                    j = i+1;
            }
            else
            {
                if(i+k>j)
                    i += k+1;
                else
                    i = j+1;
            }
        }
    
        return min(i, j);
    }
    
    
    int main()
    {
        while(scanf("%s", s1)!=EOF)
        {
            int N = strlen(s1);
    
            strcpy(s, s1);
            strcat(s, s1);
    
            FindNext(s);
            int circle = N - Next[N], time=1;
    
            if(N%circle==0)
                time = N / circle;
    
            printf("%d %d %d %d
    ", GetMin(s, N)+1, time, GetMax(s, N)+1, time);
        }
    
        return 0;
    }
    View Code
    勿忘初心
  • 相关阅读:
    Ubuntu 安装mono
    关于BigDecimal.ROUND_HALF_UP与ROUND_HALF_DOWN
    android 常用框架
    理解assign,copy,retain变strong
    SQLSERVER2008R2正确使用索引
    除非 Windows Activation Service (WAS)和万维网发布服务(W3SVC)均处于运行状态,否则无法启动网站。目前,这两项服务均处于停止状态。
    Android资源命名规范
    eclipse导入Android项目后,项目的名称变为了主Activity的名称
    日常运维管理技巧一(查看负载 W)
    Shell简介:1分钟理解什么是Shell 脚本语言 解释器 以及编译器和编译语言
  • 原文地址:https://www.cnblogs.com/YY56/p/4846489.html
Copyright © 2011-2022 走看看