Erasing Substrings CodeForces - 938F (字符串dp)
大意: 给定字符串$s$, 长度为$n$, 取$k=\lfloor log2(n)\rfloor$, 第$i$次操作删除一个长度为$2^{i-1}$的子串, 求一种方案使得, $k$次操作后$s$的字典序最小, 输出删除后的字符串.
考虑一些弱化的情况, 每次均删除长为$2$的子串, 共删除$k$次
那么很容易得出$O(n^3)$的$DP$.
int n, k;
string s, dp[N][N]; int main() {
cin>>s>>k;
n = s.size();
REP(i,1,n) REP(j,0,min(i/2,k)) {
dp[i][j] = dp[i-1][j]+s.substr(i-1,1);
if (i>=2&&j) dp[i][j] = min(dp[i][j], dp[i-2][j-1]);
}
cout<<dp[n][k]<<endl;
}
我们可以发现, $dp$过程中很多状态都是无用的, 只要一个状态的$DP$值的字典序大于另一个状态, 那么就没必要再继续往下转移了.
考虑解的结构, 解的第$i$位一定是$s[i,...,i+2k]$中的某个字符, 所以我们可以直接维护长为$2k+1$的最优转移状态, 这样就可以优化为$O(n^2)$
int n, k;
string s;
bitset<N> f; int main() {
cin>>s>>k;
if (!k) return cout<<s<<endl,0;
n = s.size();
f.set(0);
REP(i,0,n-2*k-1) {
REP(j,0,2*k) if (f[j]) f.set(j+2);
char x = 'z';
REP(j,0,2*k) if (f[j]) x=min(x,s[i+j]);
REP(j,0,2*k) if (x!=s[i+j]) f.reset(j);
putchar(x);
}
puts("");
}
而对于本题而言, 每次删除字符串总数只有$logn$, 那么状压一下即可, 长度$2^{i-1}$其实是可以改成任意的, 只不过为$2^{i-1}$时状态值恰好就等于字符串长, 方便处理一些, 完整代码如下, 复杂度是$O(kn^2)$
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, k;
char s[N];
bitset<N> f; int main() {
scanf("%s", s+1);
n = strlen(s+1);
k = log2(n), f.set(0);
int mx = (1<<k)-1, len = n-mx;
REP(i,1,len) {
char x = 'z';
REP(j,0,mx) if (f[j]) {
REP(kk,0,k-1) f.set(j|1<<kk);
}
REP(j,0,mx) if (f[j]) x = min(x, s[i+j]);
REP(j,0,mx) if (s[i+j]!=x) f.reset(j);
putchar(x);
}
puts("");
}
Erasing Substrings CodeForces - 938F (字符串dp)的更多相关文章
- Maximum Questions CodeForces - 900E (字符串,dp)
大意:给定长$n$的字符串$s$, 只含'a','b','?', '?'可以替换为任意字符, 在给定长$t$的字符串, "ababab...", 求替换尽量少的'?', 使得$s$ ...
- Three Religions CodeForces - 1149B (字符串,dp)
大意: 给定字符串S, 要求维护三个串, 支持在每个串末尾添加或删除字符, 询问S是否能找到三个不相交的子序列等于三个串. 暴力DP, 若不考虑动态维护的话, 可以直接$O(len^3)$处理出最少需 ...
- codeforces 938F(dp+高维前缀和)
题意: 给一个长度为n的字符串,定义$k=\floor{log_2 n}$ 一共k轮操作,第i次操作要删除当前字符串恰好长度为$2^{i-1}$的子串 问最后剩余的字符串字典序最小是多少? 分析: 首 ...
- Dreamoon and Strings CodeForces - 477C (字符串dp)
大意: 给定字符串$s$, $p$, 对于$0\le x\le |s|$, 求$s$删除$x$个字符后, $p$在$s$中的最大出现次数. 显然答案是先递增后递减的, 那么问题就转化求最大出现次数为$ ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- Codeforces 1150D(字符串dp)
反思 三维的dp压根没看出来,看题解以后思路又很直观,找几道字符串dp练练才行 序列自动机和优化一维略 /* __ __ * ____| |_____| |____ * | | * | __ | * ...
- 【BZOJ 2121】 (字符串DP,区间DP)
2121: 字符串游戏 Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,B ...
- AtCoder Regular Contest 081 E - Don't Be a Subsequence(字符串DP)
引用自:onion_cyc 字符串DP一直不是强项...以后没思路的题就想DP和网络流23333333 f[i]表示从i开始的后缀非子序列的最短长度 pos[i][j]表示从i开始的j字符最早出现位 ...
- NOIP2015Day2T2子串(字符串dp)
又被“if(a=b)”坑了QAQ...写C++还是得开Warning,这么久了pascal还没改过来咋回事啊QWQ 题目大意就不说了OWO 网上的题解都不怎么看得懂啊...好像写得都很乱?还是我太sb ...
随机推荐
- oracle表结构表数据导入导出
--------------------------------------imp/exp------------------------------------------------------- ...
- 哨兵模式启动redis
# 启动Redis服务器进程./redis-server ../redis.conf# 启动哨兵进程./redis-sentinel ../sentinel.conf windows 启动 redis ...
- LC 648. Replace Words
In English, we have a concept called root, which can be followed by some other words to form another ...
- LC 526. Beautiful Arrangement
uppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constr ...
- hibernate映射配置
1. 普通字段类型 2. 主键映射 单列主键映射 多列作为主键映射 主键生成策略,查看api: 5.1.2.2.1. Various additional generators 数据库: Q:一个 ...
- layui 数据表格复选框实现单选功能
//点击选中(单选)//单击行勾选checkbox事件 $(document).on("click",".layui-table-body table.layui-tab ...
- 从Docker在Linux和Windows下的区别简单理解Docker的层次结构
上篇文章我们成功在Windows下安装了Docker,输出了一个简单的Hello World程序.本文中我们将利用Docker已有的云端镜像training/webapp来发布一个简单Python的W ...
- python爬虫小实例
1.python爬取贴吧壁纸 1.1.获取整个页面数据 #coding=utf-8 import urllib def getHtml(url): page = urllib.urlopen(url) ...
- Winform之跨线程更新UI
Winform之跨线程更新UI 使用`Invoke`或者`BeginInvoke`与UI线程交互示例 参考及源码 使用Invoke或者BeginInvoke与UI线程交互示例 private void ...
- Centos7桥接网络、DNS、时间同步配置
Centos配置桥接网络.DNS服务和时间同步 1.配置桥接网络 2.配置虚拟机网卡,采用的是静态ip方式 重启network服务 3.配置dns 4.关闭防火墙和selinux 5.ping外网域名 ...