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;
    }
  • 相关阅读:
    VC++6.0在win8.1系统下运行失败的解决办法
    oracle赋值问题(将同一表中某一字段赋值给另外一个字段的语句)
    Myeclipse 2014破解教程
    关于js赋值给input解析
    java时间格式
    Idea报错Check $M2_HOME environment variable and mvn script match.
    新博客
    理解jquery的$.extend()、$.fn和$.fn.extend()
    在vs code中使用ftp-sync插件实现客户端与服务器端代码的同步
    js字符串倒序
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4852586.html
Copyright © 2011-2022 走看看