cf1121F. Compress String(后缀自动机)

Posted zwfymqz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf1121F. Compress String(后缀自动机)相关的知识,希望对你有一定的参考价值。

题意

题目链接

Sol

居然出个SAM板子也是没谁了233

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define LL long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 2e5 + 1, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '
';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, a, b, f[MAXN];
char s[MAXN];
int len[MAXN], fa[MAXN], ch[MAXN][26], las = 1, tot = 1, root = 1, siz[MAXN];
bool g[5001][5001];
void insert(int x) {
    int now = ++tot, pre = las; las = now; len[now] = len[pre] + 1; siz[now] = 1;
    for(; pre && !ch[pre][x]; pre = fa[pre]) 
        ch[pre][x] = tot;
    if(!pre) {fa[now] = root; return ;}
    int q = ch[pre][x];
    if(len[q] == len[pre] + 1) fa[tot] = q;
    else {
        int nq = ++tot; fa[nq] = fa[q]; len[nq] = len[pre] + 1; fa[q] = fa[now] = nq; 
        memcpy(ch[nq], ch[q], sizeof(ch[q]));
        for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nq;
    }
}
void GGGG() {
    for(int i = 1; i <= N; i++) {
        insert(s[i] - 'a');
        int now = root;
        for(int j = i + 1; j <= N; j++) {
            int  x = s[j] - 'a';
            if(ch[now][x]) g[j][i] = 1, now = ch[now][x];
            else break;
        }
    }
}
signed main() {
    N = read(); a = read(); b = read();
    scanf("%s", s + 1);
    GGGG();
    for(int i = 1; i <= N; i++) {
        f[i] = f[i - 1] + a;
        for(int j = i - 1; j >= 1; j--)
            if(g[i][j]) chmin(f[i], f[j] + b);
    }
    cout << f[N];
    return 0;
}
/*
6
4 4 4 4 1 7
*/

以上是关于cf1121F. Compress String(后缀自动机)的主要内容,如果未能解决你的问题,请参考以下文章

cf1200 E Compress Words(哈希)

[CF1200E] Compress Words - KMP

CF - 1117 F Crisp String

CF1063F String Journey

「CF1366G」 Construct the String「DP」

[CF1535F]String Distance