zoukankan      html  css  js  c++  java
  • 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
    */
    
  • 相关阅读:
    为什么要有binary-to-text encoding?
    海量网络存储系统原理与设计(三)
    Java中的Inner Class (一)
    海量网络存储系统原理与设计(二)
    海量网络存储系统原理与设计(一)
    [JavaScript]顺序的异步执行
    [PAT]素因子分解(20)
    [PAT]求集合数据的均方差(15)
    [PAT]数列求和(20)
    【C-001】printf理解
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10468741.html
Copyright © 2011-2022 走看看