CF1120 C. Compress String(SAM+DP)

有方程dp[i]=min(dp[i-1]+A,dp[j]+B);如果s[j+1,i]在s[i,j]中出现,所以我们就是要知道每个子串在s出现的第一个位置,这个可以hash实现或者sam实现。

pos[i][j]表示s[i,j]对应的sam的位置,occ[],表示第一次出现的位置。

#include<bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(int i=a;i>=b;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=100010;
char c[maxn],s[maxn];
struct SAM{
    int ch[maxn][26],fa[maxn],maxlen[maxn],cnt,last;
    int a[maxn],b[maxn],occ[maxn];
    void init()
    {
        cnt=last=1;
        memset(ch[1],0,sizeof(ch[1]));
        memset(occ,0x3f,sizeof(occ));
    }
    int add(int x,int pos)
    {
       int np=++cnt,p=last; last=np;  occ[np]=pos;
       maxlen[np]=maxlen[p]+1;memset(ch[np],0,sizeof(ch[np]));
       while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
       if(!p) fa[np]=1;
       else {
         int q=ch[p][x];
         if(maxlen[q]==maxlen[p]+1) fa[np]=q;
         else {
            int nq=++cnt; maxlen[nq]=maxlen[p]+1;
            fa[nq]=fa[q]; fa[q]=fa[np]=nq;
            memcpy(ch[nq],ch[q],sizeof(ch[q]));
            while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
         }
       }
    }
    void Sort()
    {
        rep(i,1,cnt) a[i]=0;
        rep(i,1,cnt) a[maxlen[i]]++;
        rep(i,1,cnt) a[i]+=a[i-1];
        rep(i,1,cnt) b[a[maxlen[i]]--]=i;
        for(int i=cnt;i>=1;i--)
            occ[fa[b[i]]]=min(occ[b[i]],occ[fa[b[i]]]);
    }
}T;
int a[maxn],A,B,dp[maxn],pos[5010][5010];
int main()
{
    int N;
    scanf("%d%d%d%s",&N,&A,&B,c+1);
    T.init();
    rep(i,1,N) T.add(c[i]-‘a‘,i);
    rep(i,1,N) {
        int now=1;
        rep(j,i,N){
            now=T.ch[now][c[j]-‘a‘];
            pos[i][j]=now;
        }
    }
    T.Sort();
    rep(i,1,N){
        dp[i]=dp[i-1]+A;
        rep(j,1,i-1){
            if(T.occ[pos[j+1][i]]<=j){
                dp[i]=min(dp[i],dp[j]+B);
                break;
            }
        }
    }
    printf("%d\n",dp[N]);
    return 0;
}

原文地址:https://www.cnblogs.com/hua-dong/p/10468652.html

时间: 2024-11-25 01:16:02

CF1120 C. Compress String(SAM+DP)的相关文章

NYOJ 1067 Compress String(区间dp)

Compress String 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描述 One day,a beautiful girl ask LYH to help her complete a complicated task-using a new compression method similar to Run Length Encoding(RLE) compress a string.Though the task is difficult, LYH is

spoj 1812 LCS2(SAM+DP)

Longest Common Substring II Time Limit: 236MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the set of lowercase letters. Substring,

hdu 3336 count the string(KMP+dp)

题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[i]表示以i为结尾包含前缀的数量,则dp[i]=dp[next[i]]+1,最后求和即可. #include <map> #include <set> #include <list> #include <cmath> #include <queue>

bnu oj 34985 Elegant String (矩阵+dp)

Elegant String We define a kind of strings as elegant string: among all the substrings of an elegant string, none of them is a permutation of "0, 1,-, k". Let function(n, k) be the number of elegant strings of length n which only contains digits

poj2282(数位dp)

题意:计算a-b中各个数字出现的个数: 解法:数位dp(思想都是先算1-b的个数,然后减掉1-a中的个数),1-9数字的计算和前边计算1的那一篇数位dp差不多,计算0时候要加一维表示前缀是否全是0: 代码: /****************************************************** * author:xiefubao *******************************************************/ #pragma comment

hdu 1011(树形dp)

Mark.看着吴神博客写的,还未完全懂. 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <set> 8 #include <map> 9 #include <string>

POJ 2342 (树形DP)

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure

hdu 3709 Balanced Number (数位dp)

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1871    Accepted Submission(s): 836 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

light oj 1422 - Halloween Costumes (区间dp)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it's Ha