Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希
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 + 二分 + 字符串哈希的更多相关文章
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...
- Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)
题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...
- 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 ...
- Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)
F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
随机推荐
- UVa 548 Tree(二叉树最短路径)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- [剑指Offer]42-连续子数组的最大和(DP)
题目链接 https://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484?tpId=13&tqId=11183&t ...
- Redis 非关系性数据库集群的搭建与常用方法
redis 非关系型数据库,内存型数据库,现在大家都不陌生了,无论大中小型企业,都会将redis应用到自己的项目中,以此来减轻数据库的压力 安装步骤: 1.安装gcc 安装c语言的编译环境 yum i ...
- c#发送短信
短息计费平台:http://sms.webchinese.cn/User/?action=key 代码: using System;using System.Collections.Generic;u ...
- wget: command not found
支持yum yum -y install wget 不支持yum a.查看当前linux版本 cat /etc/issue && uname -a b.根据版本下载相应的rpm htt ...
- spring + ibatis 多数据源事务(分布式事务)管理配置方法(转)
spring + ibatis 多数据源事务(分布式事务)管理配置方法(转) .我先要给大家讲一个概念:spring 的多数据源事务,这是民间的说法.官方的说法是:spring 的分布式事务.明白了这 ...
- (转)ScriptManager.RegisterStartupScript方法和Page.ClientScript.RegisterStartupScript() 方法
ScriptManager.RegisterStartupScript方法 如果页面中不用Ajax,cs中运行某段js代码方式可以是: Page.ClientScript.RegisterStartu ...
- C#程序如何以管理员身份运行
VISTA 和 Windows 7 都使用了UAC来控制程序访问,对于一些需要使用管理员身份运行的程序就得右键以管理员身份运行. C# 编程中可以使程序自动使用管理员身份运行,也就是我们常常看到一些程 ...
- 部署描述符(web.xml)和标注(annotation)
部署描述符(web.xml) 详细信息可在http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html上下载web- ...
- maven3 学习
主要参考博文:http://www.cnblogs.com/yjmyzz/p/3495762.html 修正: 1.下载maven 3.1.1 先到官网http://maven.apache.org/ ...