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,或者kmp实现。
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=;
char c[maxn],s[maxn];
struct SAM{
int ch[maxn][],fa[maxn],maxlen[maxn],cnt,last;
int a[maxn],b[maxn],occ[maxn];
void init()
{
cnt=last=;
memset(ch[],,sizeof(ch[]));
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]+;memset(ch[np],,sizeof(ch[np]));
while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
if(!p) fa[np]=;
else {
int q=ch[p][x];
if(maxlen[q]==maxlen[p]+) fa[np]=q;
else {
int nq=++cnt; maxlen[nq]=maxlen[p]+;
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,,cnt) a[i]=;
rep(i,,cnt) a[maxlen[i]]++;
rep(i,,cnt) a[i]+=a[i-];
rep(i,,cnt) b[a[maxlen[i]]--]=i;
for(int i=cnt;i>=;i--)
occ[fa[b[i]]]=min(occ[b[i]],occ[fa[b[i]]]);
}
}T;
int a[maxn],A,B,dp[maxn],pos[][];
int main()
{
int N;
scanf("%d%d%d%s",&N,&A,&B,c+);
T.init();
rep(i,,N) T.add(c[i]-'a',i);
rep(i,,N) {
int now=;
rep(j,i,N){
now=T.ch[now][c[j]-'a'];
pos[i][j]=now;
}
}
T.Sort();
rep(i,,N){
dp[i]=dp[i-]+A;
rep(j,,i-){
if(T.occ[pos[j+][i]]<=j){
dp[i]=min(dp[i],dp[j]+B);
break;
}
}
}
printf("%d\n",dp[N]);
return ;
}
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 he ...
- spoj 1812 LCS2(SAM+DP)
[题目链接] http://www.spoj.com/problems/LCS2/en/ [题意] 求若干个串的最长公共子串. [思路] SAM+DP 先拿个串建个SAM,然后用后面的串匹配,每次将所 ...
- hdu_3336: Count the string(KMP dp)
题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- 【HDU 3336】Count the string(KMP+DP)
Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...
- [HAOI2016]找相同字符(SAM+DP)
感觉很水. 因为SAM上一个点的子树大小代表这个点所表示子串的出现次数. 建出广义后缀自动机之后.在\(parent\)树上跑\(DP\),维护\(size[i][1]\),和\(size[i][0] ...
- F. Clear the String(区间 DP )//每次都删除一个相同字符的子串 , 最小多少次
https://codeforces.com/contest/1132/problem/F 借鉴:https://www.cnblogs.com/chhokmah/p/10508762.html 题意 ...
- 2018.12.12 codeforces 931E. Game with String(概率dp)
传送门 感觉这题难点在读懂题. 题目简述:给你一个字符串s,设将其向左平移k个单位之后的字符串为t,现在告诉你t的第一个字符,然后你可以另外得知t的任意一个字符,求用最优策略猜对k的概率. 解析: 预 ...
- 【Hihocoder1413】Rikka with String(后缀自动机)
[Hihocoder1413]Rikka with String(后缀自动机) 题面 Hihocoder 给定一个小写字母串,回答分别把每个位置上的字符替换为'#'后的本质不同的子串数. 题解 首先横 ...
随机推荐
- cpu-z for ubuntu 12.04 64bit : cpu-g
wget https://launchpad.net/~phantomas/+archive/ppa/+files/cpu-g_0.9.0_amd64.deb sudo dpkg -i cpu-g*. ...
- ssh免输入密码登录
ssh免输入密码登录 ubuntu下生成ssh密钥参见. https://confluence.atlassian.com/display/BITBUCKET/Use+the+SSH+prot ...
- ROM、PROM、EPROM、EEPROM、FLASH ROM、FLASH、eMMC
ROM(Read Only Memory,只读存储器)芯片:在微机的发展初期,BIOS都存放在ROM芯片中.ROM内部的资料是在ROM的制造工序中,在工厂里用特殊的方法被烧录进去的,其中的内容只能读不 ...
- OOP⑷
1.对象数组: /** *学生类 */ public class Student { // 创建一个对象数组保存3名学生的信息 Student[] stus = new Student[3]; int ...
- nginx在linux上的安装与配置详解(一)
Nginx的安装与配置详解 (1)nginx简介 nginx概念: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like ...
- jenkins部署web项目到tomcat(五)
(1)maven构建web项目 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- flask项目结构(一)mariadb
简介: 本文主要是根据自己所学,创建一个flask项目,使用sqlalchemy,alembic,mariadb,bootstrap,APScheduler,selenium,request…………技 ...
- sass的加减乘除
运算 时单位要一样 否则报错 除法时 要加() width: (100px / 2); 符号在已有的数学表达式中时,也会被认作除法符号,就不要加() .box { width: 100px / 2 + ...
- 查看linux 内核版本信息
uname -r2.6.32-696.el6.x86_64uname -ix86_64
- POJ 2407 Relatives(欧拉函数入门题)
Relatives Given n, a positive integer, how many positive integers less than n are relatively prime t ...