https://codeforces.com/contest/1121/problem/F

题意

给你一个有n(<=5000)个字符的串,有两种压缩字符的方法:

1. 压缩单一字符,代价为a

2. 压缩一个串,条件是这个串是前面整个串的连续子串,代价为b

题解

  • n<=5000
  • 定义dp[i]为压缩前i个字符的代价,则答案为dp[n]
  • dp[i]=min(dp[i-1]+a,min(dp[j]+b)(即[j+1,i]为[1,j]的子串))
  • 用字符串哈希处理判定一个串是否为前面的子串

坑点

  • 串abab,如何判定后面一个ab是前面ab的子串?

    • 照旧给每一个位置加权
    • 判定的时候,首先将权值加到同一个权级再比较

      比如存在一个首字符在i和一个首字符在j的串,那么比较的时候哈希值分别都要乘以(size-i)和(size-j),得到权级都是size的串

  • 两层for已经是n*n复杂度,还需要判定后面的串是否是前面串的子串?
    • 一开始想法就是用一个map[i]记录每个位置之前哈希值的出现次数,但是会超内存
    • 换一个\(n*n*log(n)\)的算法,二分找出最小的位置\(log(n)\),枚举前面每一个位置固定长度串\(n\)

代码

#include<bits/stdc++.h>
#define P 47 //加权的质数较小
#define mod 1000000003 //哈希表的质数较大
#define ll long long
using namespace std;
int n,a,b,i,j,sum[5005],dp[5005],pw[6000],l,mid,r;
char s[5005]; void init(){
pw[0]=1;
for(int i=1;i<=n+100;i++)
pw[i]=(ll)pw[i-1]*P%mod;
for(int i=1;i<=n;i++)
sum[i]=(sum[i-1]+(ll)pw[i]*(s[i]-'a'+1)%mod)%mod;
} int geths(int i,int j){
return (int)(((ll)sum[j]-sum[i-1]+mod)%mod*pw[n+50-i]%mod);
} int ck(int p,int i,int j,int len){
int hs=geths(i,j);
for(int k=1;k<=p-len+1;k++){
if(geths(k,k+len-1)==hs)return 1;
}
return 0;
} int main(){
cin>>n>>a>>b;
scanf("%s",s+1);
init();
dp[0]=0;
for(i=1;i<=n;i++){
dp[i]=dp[i-1]+a;
l=1;r=i-1;
while(l<r){
mid=(l+r)/2;
if(ck(i-mid,i-mid+1,i,mid))l=mid+1;
else r=mid;
}
while(!ck(i-l,i-l+1,i,l))l--;
if(l>=1)dp[i]=min(dp[i-l]+b,dp[i]);
}
cout<<dp[n];
}

Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希的更多相关文章

  1. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  2. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  3. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  4. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  5. Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)

    题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...

  6. Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)

    题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...

  7. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  8. Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

    F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...

  9. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

随机推荐

  1. java编程 求和

    用java编程,实现字符串强制类型转化成整数型,用到Integer.parseInt(),可以把字符串强制转换成整数 结果截图

  2. 【nginx】大文件下载

    nginx自带文件读取功能,而且实现地很好. 比如直接读取txt文件,png图片等,用chrome可以直接获取到内容. 但是对于很大的文件,比如有2个G的视频,nginx如何吐出2G的内容呢? 实验: ...

  3. VS unable to update auto-refresh path。。。。

    手工创建提示报错的路径,重新生成,成功

  4. [leetcode]449. Serialize and Deserialize BST序列化与反序列化BST

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  5. 我的第一个博客——Fragment遇到的问题

    最近项目中使用fragment时遇到了一些问题: 1.fragment的刷新问题. 解决:我的情况是有多个fragment时,只需要刷新其中几个界面.之前我在网上看到的一些方法.如下: 首先在Adap ...

  6. InstallShield 2015 安装 在vs2015

     网上很少注册InstallShield 2015  的方法,而且很多以前版本的注册也很笼统,今天我就说说几个细节上的问题.相信大家看了会有帮助,有问题回帖,我会及时跟上, 先说说我遇到的问题 安装: ...

  7. 哪些intel 网卡支持SR-IOV

    哪些英特尔®以太网适配器和控制器支持 SR-IOV? 英特尔®以太网融合网络适配器 X710 系列 英特尔®以太网聚合网络适配器 X710-da2 英特尔®以太网聚合网络适配器 X710-da4 英特 ...

  8. Spring IOC(五)依赖注入

    Spring IOC(五)依赖注入 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.autowire 五种注入方式测试 ...

  9. 室内设计类网站Web原型制作分享——Dinzd

    Dinzd是一家德国室内设计网站,网站内涵盖全球设计精品资讯以及优秀案列.网站布局简单直观,内容丰富. 此原型模板所用到的交互动作有结合弹出面板做下拉菜单效果,鼠标按下文字按钮跳转页面,按钮hover ...

  10. npm run build出问题十分通用的解决方法

    1.C:\NanoFabric\52ABP\SPAHost\ClientApp\node_modules 原来的目录重命名为C:\NanoFabric\52ABP\SPAHost\ClientApp\ ...