• HDU 3374 String Problem (KMP+最大最小表示)


    HDU 3374 String Problem (KMP+最大最小表示)

    String Problem

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


    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
     
     
    这个题我开始直接做,感觉很容易,但WA
     
    WA代码

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;

    char str[1000005];
    char str1[1000005];
    int len;

    int main(void)
    {
    int i;
    int a1,a2;
    while(scanf("%s",str)==1)
    {
    strcpy(str1,str);
    len=strlen(str);
    sort(str,str+len);
    int k1=0,k2=0;
    for(i=0;str[i]!='';i++)
    {
    if(str[i]==str[0])
    k1++;
    if(str[i]==str[len-1])
    k2++;
    }
    for(i=0;str1[i]!='';i++)
    {
    if(str1[i]==str[0])
    {
    a1=i;
    break;
    }
    }
    for(i=0;str1[i]!='';i++)
    {
    if(str1[i]==str[len-1])
    {
    a2=i;
    break;
    }
    }
    printf("%d %d %d %d ",a1+1,k1,a2+1,k2);
    }
    return 0;
    }

    不知道哪里错了,如果谁知道,请留言!

    后来到网上搜了一下发现别人是用next数组,根据l=len-next[len]为最小字符串找个数,根据最小表示法,与最大表示法,找初始位置。

    //最小表示法

    int minexp(char *s,int x)//x=strlen(s);
    {
    int i=0,j=1,k=0,t;
    while(i<x&&j<x&&k<x)
    {
    t=s[(i+k)%x]-s[(j+k)%x];
    if(t==0) k++;
    else
    {
    if(t>0)
    i+=k+1;
    else
    j+=k+1;
    if(i==j)
    j++;
    k=0;
    }
    }
    return i<j?i:j;
    }

     
     
    //最大表示法
     

    int maxexp(char *s,int x)
    {
    int i=0,j=1,k=0,t;
    while(i<x&&j<x&&k<x)
    {
    t=s[(i+k)%x]-s[(j+k)%x];
    if(t==0) k++;
    else
    {
    if(t<0) i+=k+1; //这里是最小表示法与最大表示法的区别。
    else j+=k+1;
    if(i==j) j++;
    k=0;
    }
    }
    return i<j?i:j;
    }

    AC代码

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;

    char pat[1000005];
    int lenp,next[1000005];

    void get_next()
    {
    int i,j;
    next[0]=-1;
    i=0;
    j=-1;
    while(i<lenp)
    {
    if(j==-1||pat[i]==pat[j])
    {
    i++;
    j++;
    next[i]=j;
    }
    else
    j=next[j];
    }
    }
    //最小表示
    int minexp(char *s,int x)
    {
    int i=0,j=1,k=0,t;
    while(i<x&&j<x&&k<x)
    {
    t=s[(i+k)%x]-s[(j+k)%x];
    if(t==0) k++;
    else
    {
    if(t>0)
    i+=k+1;
    else
    j+=k+1;
    if(i==j)
    j++;
    k=0;
    }
    }
    return i<j?i:j;
    }

    //最大表示
    int maxexp(char *s,int x)
    {
    int i=0,j=1,k=0,t;
    while(i<x&&j<x&&k<x)
    {
    t=s[(i+k)%x]-s[(j+k)%x];
    if(t==0) k++;
    else
    {
    if(t<0) i+=k+1; //这里是区别
    else j+=k+1;
    if(i==j) j++;
    k=0;
    }
    }
    return i<j?i:j;
    }

    int main()
    {
    int ans,i,j,a,b;
    while(scanf("%s",pat)!=EOF)
    {
    lenp=strlen(pat);
    get_next();
    a=minexp(pat,lenp);
    b=maxexp(pat,lenp);
    int l=lenp-next[lenp];
    if(lenp%l==0)
    {
    ans=lenp/l;//cout<<lenp/l<<endl;;
    }
    else
    {
    ans=1;//printf("1 ");
    }
    printf("%d %d %d %d ",a+1,ans,b+1,ans);
    }
    return 0;
    }

  • 相关阅读:
    Java 练习(获取两个字符串中最大相同子串)
    STM32F103 实现 简易闹钟小程序
    STM32F103 实现 LCD显示年月日时分秒星期 并可逐值修改的日期 小程序
    Docker报错之“Failed to get D-Bus connection: Operation not permitted”
    数据结构解析
    每天一条DB2命令-004
    每天一条DB2命令-003
    每天一条DB2命令-002
    ElasticSearch系列
    模块三 GO语言实战与应用-BYTES包与字节串操作(下)
  • 原文地址:https://www.cnblogs.com/liudehao/p/3909485.html
Copyright © 2020-2023  润新知