zoukankan      html  css  js  c++  java
  • HDOJ3374 String Problem 【KMP】+【最小表示法】

    String Problem

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


    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

    这题之前做过一次。今天做的时候还是把最小表示法给搞忘了,用普通枚举求最小字典序,果断超时。

    题意:给定一个串。原始串序号为1,每向左移动一次序号加一,求最小字典序的序号和最大字典序的序号以及循环次数。

    题解:求循环次数仅仅须要找到循环节点,难点是找最小(大)字典序的序号,要用到最小表示法,这个就当成模板来用吧。最小表示法难理解的地方在s[i+k]>s[j+k]时i须要+=k+1,这是由于s[i...i+k-1]和s[j...j+k-1]相等。说明不论什么以s[i...i+k]開始的字符串都要大于以s[j]開始的字符串。

    #include <stdio.h>
    #define maxn 1000002
    
    char str[maxn];
    int next[maxn], len, cir, minPos, maxPos;
    
    void getNext()
    {
        int i = 0, j = -1;
        next[0] = -1;
        while(str[i]){
            if(j == -1 || str[i] == str[j]){
                ++i; ++j;
                next[i] = j; //mode 1
            }else j = next[j];
        }
        len = i;
    }
    
    void findMinAndMaxPos()
    {
        minPos = maxPos = 0;
        int k = 0, pos = 1, t;
        while(minPos < len && k < len && pos < len){
            t = str[(minPos + k) % len] - str[(pos + k) % len];
            if(t == 0){ ++k; continue; }
            else if(t < 0) pos += k + 1;
            else minPos += k + 1;
            k = 0;
            if(minPos == pos) ++pos;
        }
        if(minPos > pos) minPos = pos;
        ++minPos;
    
        k = 0; pos = 1;
        while(maxPos < len && k < len && pos < len){
            t = str[(maxPos + k) % len] - str[(pos + k) % len];
            if(t == 0){ ++k; continue; }
            else if(t > 0) pos += k + 1;
            else maxPos += k + 1;
            k = 0;
            if(maxPos == pos) ++pos;
        }
        if(maxPos > pos) maxPos = pos;
        ++maxPos;
    }
    
    int main()
    {
        //freopen("stdin.txt", "r", stdin);
        while(scanf("%s", str) != EOF){
            getNext(); 
            findMinAndMaxPos(); cir = 1;
            if(len % (len - next[len]) == 0) 
                cir = len / (len - next[len]);
            printf("%d %d %d %d
    ", minPos, cir, maxPos, cir);
        }
        return 0;
    }



  • 相关阅读:
    数据结构考研--线性表例2-4
    2011 BENELLI TRE1130K所有系统诊断:OBDSTAR MS80或OBDSTAR MS50?
    BMW E60可以通过Autel IM508和XP400 Pro读取数据并学习密钥吗?
    Launch X431 V V4.0 2021最新升级:32GB存储+ 30特殊功能
    Autel IM608 Pro为BMW 2010 535i添加新钥匙
    Xhorse VVDI Prog V5.0.0软件免费下载
    使用XP400 Pro通过Autel IM508读取Benz W207 EIS数据
    由CGDI Prog BMW与GODIAG GT100进行的BMW FEM / BDC密钥编程
    BMW CAS4 / CAS4 +编程测试平台购买建议
    2021 GODIAG GT100 ECU接线盒评测:出色的诊断和节省维修成本的设备
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5222604.html
Copyright © 2011-2022 走看看