HDU 3374 exkmp + Minimum Maximum string representation

The meaning of problems

Find a minimum string appears first (large) represents the position, and the minimum (large) number of occurrences of the string represented by

analysis

Is represented by the minimum (large) method to obtain the minimum appears first (large) indicates the position and length of the string to find occurrences enlarged twice with exkmp.

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define pb push_back
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=2e6+10;
char s[maxn];
int n,nex[maxn];
int Get_min()
{
    int i = 0,j = 1,k = 0,t;
    while(i < n && j < n && k < n)
    {
        t = s[(i+k)%n] - s[(j+k)%n];
        if(!t) k++;
        else
        {
            if(t > 0) i += k+1;
            else j += k+1;
            if(i == j) j++;
            k = 0;
        }
    }
    return i >j ?j :i;
}
int Get_max()
{
    int i = 0,j = 1,k = 0,t;
    while(i < n && j < n && k < n)
    {
        t = s[(i+k)%n] - s[(j+k)%n];
        if(!t) k++;
        else
        {
            if(t > 0) j += k+1;
            else i += k+1;
            if(i == j) i++;
            k = 0;
        }
    }
    return i >j ?j :i;
}
int exkmp(int pos){
    int i=pos+1,j=0,p0=pos+1,cnt=1;
    while(i<2*n&&j<n&&s[i]==s[j+pos]) ++i,++j;
    nex[p0]=j;if(j==n) ++cnt;
    for(int i=pos+2;i<n;i++){
        j=max(0,min(nex[p0]+p0-i,nex[i-p0+pos]));
        if(i+j<nex[p0]+p0){
            nex[i]=j;
        }else{
            int k=i+j;
            while(k<2*n&&j<n&&s[k]==s[j+pos]) ++k,++j;
            nex[i]=j;p0=i;
        }
        if(nex[i]==n) ++cnt;
    }
    return cnt;
}
int main(){
    //ios::sync_with_stdio(false);
    //freopen("in","r",stdin);
    while(~scanf("%s",s)){
        n=strlen(s);
        for(int i=n;i<2*n;i++) s[i]=s[i-n];
        int j=Get_min();
        printf("%d %d ",j+1,exkmp(j));
        j=Get_max();
        printf("%d %d\n",j+1,exkmp(j));
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/xyq0220/p/11495146.html