zoukankan      html  css  js  c++  java
  • hdu 3374 String Problem(kmp+最小表示法)

    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

    题意:已知字符串 s 现在要求出求 每个字符串的最小循环表示,最大循环表示(输出序号最小的)以及该字符串在其中的次数。

    思路:最小表示法 输出序号 next[]数组输出 该字符串出现的次数(其实就是字符串的循环节) 

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<stack>
    #include<bitset>
    #include<cstdlib>
    #include<cmath>
    #include<set>
    #include<list>
    #include<deque>
    #include<map>
    #include<queue>
    #define ll long long int
    using namespace std;
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
    int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
    const int inf=0x3f3f3f3f;
    const ll mod=1e9+7;
    int nextt[1000000+7];
    void getnext(string s){
        nextt[1]=0;
        int len=s.length();
        for(int i=2,j=0;i<=len;i++){
            while(j>0&&s[i-1]!=s[j]) j=nextt[j];
            if(s[i-1]==s[j]) j++;
            nextt[i]=j;
        }
    }
    int get_min(string s){
        int len=s.length();
        int i=0; int j=1; int k=0;
        while(i<len&&j<len&&k<len){
            int t=s[(i+k)%len]-s[(j+k)%len];
            if(!t) k++;
            else{
                if(t>0) i+=(k+1); //s[i]>s[j] i推进 
                else j+=(k+1);
                if(i==j) j++;
                k=0;
            }
        }
        return i>j?j:i;
    }
    int getmax(string s){
        int len=s.length();
        int i=0; int j=1; int k=0;
        while(i<len&&j<len&&k<len){
            int t=s[(i+k)%len]-s[(j+k)%len];
            if(!t) k++;
            else{
                if(t>0) j+=(k+1);
                else i+=(k+1);
                if(i==j) j++;
                k=0;
            }
        }
        return i>j?j:i;
    }
    int main(){
        ios::sync_with_stdio(false);
        string s;
        while(cin>>s){
            getnext(s);
            int len=s.length();
            int ans=(len%(len-nextt[len])==0)?(len/(len-nextt[len])):1; //有循环节ans为循环节个数 不然为1 
            cout<<get_min(s)+1<<" "<<ans<<" "<<getmax(s)+1<<" "<<ans<<endl;
        } 
        return 0;
    }
  • 相关阅读:
    记一次文件上传远程服务器问题
    Python模块——loguru日志模块简单学习
    Python使用百度地图API根据地名获取相应经纬度
    Python用正则表达式匹配汉字
    【工具】下载与使用(感谢博友)
    【umask】安装产品,脚本中创建目录后注意刷权限
    【凝聚】引荐优笔
    【解决办法1】centos75 No manual entry for XXXX in section X
    【填坑1】CentOS7 systemctrl管理的服务,open files的神坑
    错题记录(二)
  • 原文地址:https://www.cnblogs.com/wmj6/p/10516503.html
Copyright © 2011-2022 走看看