L - Finding the Bases

Topic links:

https://cn.vjudge.net/contest/313499#problem/L

 

SOLUTION:

 

 

For the meaning of the questions, the shortest required expression, when used to represent a minimum string-membered cycle, which was the minimum expression.
For the next string S is obtained from the matching array analysis it can be found: when the i-next [i] divisible i, S [1 ~ i- next [i]] is the smallest element can cycle S [1 ~ i] a. It is the maximum number of cycles i / (i-next [i ]).
Next to enumerate all strings minimum cycle yuan, take the best.
 
 CODE:

#include "bits/stdc++.h"

using namespace std;

const int maxn = 1e4 + 100;


int n;
char s[maxn];
int Next[maxn];
int f[maxn];

void getnext(char str[], int l) {
    for (int i = 2, j = 0; i <= l; i++) {
        while (j > 0 && str[i] != str[j + 1]) j = Next[j];
        if (str[i] == str[j + 1]) j++;
        Next[i] = j;
    }
}


int main() {
    //freopen("input.txt", "r", stdin);
    int N, now, temp;
    scanf("%d", &N);
    while (N--) {
        scanf("%s", s + 1);
        n = strlen(s + 1);
        for (int i = 0; i <= n; i++)
            f[i] = i;
        for (int i = 1; i <= n; i++) {
            getnext(s + i - 1, n - i + 1);
            for (int j = i; j <= n; j++) {
            f[j]=min(f[j],j-i+1+f[i-1]);
                now = j - i + 1;
                if (now % (now - Next[now]) == 0) {
                    f[j] = min(f[j], f[i - 1] + now - Next[now]);
                }
            }
        }
        printf("%d\n", f[n]);
    }
    return 0;
}

  


 

 

Guess you like

Origin www.cnblogs.com/zhangbuang/p/11279023.html