当前位置:网站首页>N - string problem HDU - 3374 (max min notation template)

N - string problem HDU - 3374 (max min notation template)

2022-06-21 21:29:00 fighting_ yifeng

String Problem HDU - 3374

The question : Give you a string , Let you find the maximum and minimum position of his loop string , And the number of occurrences .

analysis : Good number of occurrences please , You can imagine that if the maximum and minimum appear multiple times, there is a circular string , It is possible to have multiple maxima and minima . Then save the template of the maximum and minimum representation .

#include <iostream>
#include <cstdio>
#include <stack>
#include <cmath>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
const int maxn = 1000010;

int next1[maxn], n, m, t;
char x[maxn], y[maxn];
void kmp_pre(int m)
{
    int i, j;
    j = -1;next1[0] = -1;
    i = 0;
    while(i < m){
        while(-1 != j && x[i] != x[j]) j = next1[j];
        next1[++i] = ++j;
    }
}
int minmum(int len) //  Minimum representation 
{
    int i = 0, j = 1, k = 0;
    while(i < len && j < len && k < len){
        int temp = x[(i + k) % len] - x[(j + k) %len];
        if(temp == 0) k++;
        else{
            if(temp > 0) i = i + k + 1;
            else j = j + k + 1;
            if(i == j) j++;
            k = 0;
        }
    }
    return i < j ? i : j;
}
int  maxmum(int len) //  The maximum representation 
{
    int i = 0, j = 1, k = 0;
    while(i < len && j < len && k < len){
        int temp = x[(i + k) % len] - x[(j + k) %len];
        if(temp == 0) k++;
        else{
            if(temp > 0) j = j + k + 1;
            else i = i + k + 1;
            if(i == j) j++;
            k = 0;
        }
    }
    return i < j ? i : j;
}
int main()
{
    while(~scanf("%s", x))
    {
        m = strlen(x);
        kmp_pre(m);
        int num = 1, len = m - next1[m];

        if(m % len == 0)
            num = m / len;
        int minn = minmum(m);
        int maxx = maxmum(m);
        printf("%d %d %d %d\n", minn + 1, num, maxx + 1, num);
    }
}

 

原网站

版权声明
本文为[fighting_ yifeng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211939197512.html