当前位置: 移动技术网 > IT编程>开发语言>C/C++ > cf1121F. Compress String(后缀自动机)

cf1121F. Compress String(后缀自动机)

2019年03月05日  | 移动技术网IT编程  | 我要评论

大名府,小孩适合买哪种保险,生活常识趣味问答

题意

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 << '\n';}
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
*/

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网