[Xjoi1898] [hdu4333] Revolving Digits, xjoi1898hdu4333

Source: Internet
Author: User

[Xjoi1898] [hdu4333] Revolving Digits, xjoi1898hdu4333

/* Note: This is not the original hdu4333, but a simplified version. The original version has multiple groups of data. However, this code can still pass multiple groups of data after the input is modified */

 

Returns a number string and asks how many essentially different homogeneous strings are smaller, the same, and big than the original string.
Homogeneous string refers to the conversion from the original string S [1-N] To the homogeneous string S [I-N] + S [0-i-1] through rotation.
Different in nature means different strings.

Input Format: output format of one row of several strings: one row. Output of essentially different homogeneous strings is smaller than the original string, the same, and the large three numbers are separated by a space. Sample input:
123123
Sample output:
0 1 2
Data range: Number String Length <= 100000 first, we use KMP to obtain the next array nex in KMP. What is the purpose? The function is to find the number of cyclic segments of the original string, so as to ensure that the Count string is essentially different. Then, set the original string to s2, and the string s1 = s2 + s2. Run EKMP with s1 as the parent string and s2 as the Child string. Finally, for each ex [I], the length greater than or equal to s2 is equal to the number of components starting from position I, otherwise, you only need to compare s [I] And s [I + next [I] (next of EKMP ).
#include<cstdio>#include<cstring>using namespace std;char s1[1000100],s2[1000100];int ex[1000100],nxt[1000100],nex[1000100];void get(char *s2){    int m=strlen(s2);    nex[0]=nex[1]=0;int j=0;    for(int i=1;i<m;i++){        while(j>0&&s2[i]!=s2[j])j=nex[j];        if(s2[i]==s2[j])j++;        nex[i+1]=j;    }}void getex(char *s2){    int j=0,n=strlen(s2);    while(j+1<n&&s2[j+1]==s2[j])j++;    nxt[0]=n;nxt[1]=j;int po=1,p=nxt[1]+1;    for(int i=2;i<n;i++){        int len=nxt[i-po];        if(len+i<p)nxt[i]=len;        else{            int j=p-i;            if(j<0)j=0;            while(i+j<n&&s2[j]==s2[j+i])j++;            nxt[i]=j;            po=i;            p=nxt[po]+po;        }    }}void exkmp(char *s1,char *s2){    int j=0,n=strlen(s1),m=strlen(s2);    while(s1[j]==s2[j]&&j<n&&j<m)j++;    ex[0]=j;int po=0,p=ex[0];    for(int i=1;i<n;i++){        int len=nxt[i-po];        if(len+i<p)ex[i]=len;        else{            int j=p-i;            while(i+j<n&&j<m&&s1[j+i]==s2[j])j++;            ex[i]=j;            po=i;            p=ex[po]+po;        }    }}int main(){    scanf("%s",s2);int len=strlen(s2);    for(int i=0;i<len;i++)    s1[i]=s2[i],s1[i+len]=s2[i];    getex(s2);    exkmp(s1,s2);    get(s2);        int temp=len%(len-nex[len])==0?len/(len-nex[len]):1;    int a=0,b=0,c=0;    for(int i=0;i<len;i++){        if(ex[i]>=len)b++;        else if(s1[i+ex[i]]<s2[ex[i]])a++;        else c++;    }    printf("%d %d %d",a/temp,b/temp,c/temp);}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.