D1. Kirk and a Binary String (easy version)

01串找最长不降子序列

给定字符串s,要求生成一个等长字符串t,使得任意l到r位置的最长不降子序列长度一致

从后往前暴力枚举,枚举每个一替换成0后是否改变了l到r位置的最长不降子序列长度

01串的最长不降子序列,可以通过线性dp求解

dp i表示以i结尾的最长不降子序列长度

dp[0]=dp[0]+s[i]=='0';

dp[1]=max(dp[0],dp[1])+s[i]=='1';

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sc(x) scanf("%I64d",&(x));
typedef long long ll;
#define maxn 2005 #define INF 1e18
ll N;
ll val[][maxn];
ll dp[];
void LIS(string s,int st,int val[])
{
dp[]=dp[]=;
for(int i=st;i<N;i++){
if(s[i]==''){
dp[]++;
}else dp[]=max(dp[],dp[])+;
val[i]=max(dp[],dp[]);
} }
signed main()
{
string s,t;
cin>>s;
N=s.size();
t=s;
//int len=0;
for(int i=N-; i>=;i--)
{
if(s[i]=='')
{
t[i]='';
LIS(s,i,val[]);
LIS(t,i,val[]);
for(int j=i;j<N;j++){
if(val[][j]!=val[][j]){
t[i]='';
break;
}
}
}
}
cout<<t<<'\n';
}

下面这个大概思路是从后往前枚举,后面的0个数比1个数小时,可以删当前位置1

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sc(x) scanf("%I64d",&(x));
typedef long long ll;
#define maxn 2005 #define INF 1e18
ll N;
ll val[][maxn];
ll dp[];
void LIS(string s,int st,int val[])
{
dp[]=dp[]=;
for(int i=st;i<N;i++){
if(s[i]==''){
dp[]++;
}else dp[]=max(dp[],dp[])+;
val[i]=max(dp[],dp[]);
} }
signed main()
{
string s,t;
cin>>s;
N=s.size();
t=s;
int cnt=;
for(int i=N-; i>=;i--)
{
if(s[i]==''){
cnt++;
}else if(cnt==&&s[i]==''){
t[i]='';
}else cnt--;
}
cout<<t<<'\n'; }

D1. Kirk and a Binary String (easy version)的更多相关文章

  1. D2. Kirk and a Binary String (hard version) D1 Kirk and a Binary String (easy version) Codeforces Round #581 (Div. 2) (实现,构造)

    D2. Kirk and a Binary String (hard version) time limit per test1 second memory limit per test256 meg ...

  2. Codeforces 1204D2. Kirk and a Binary String (hard version) (dp思路)

    题目链接:http://codeforces.com/contest/1204/problem/D2 题目是给定一个01字符串,让你尽可能多地改变1变为0,但是要保证新的字符串,对任意的L,R使得Sl ...

  3. 01串LIS(固定串思维)--Kirk and a Binary String (hard version)---Codeforces Round #581 (Div. 2)

    题意:https://codeforc.es/problemset/problem/1204/D2 给你一个01串,如:0111001100111011101000,让你改这个串(使0尽可能多,任意 ...

  4. CF1204D Kirk and a Binary String

    题目链接 problem 给出一个长度为\(n(n\le 10^5)\)的只包含01的字符串.把尽可能多的1变为0,使得对于所有的\(l \in [1,n],r\in [l,n]\),区间\([l,r ...

  5. Codeforces 1204D Kirk and a Binary String - 数学

    题目传送门 传送门 群除我均会猜结论/找规律,sad.... 以下内容只保证代码能过system test,证明应该都是在纯口胡 约定下文中的$LIS$表示最长不下降子序列. 定义$zero(s)$表 ...

  6. CF #579 (Div. 3) D1.Remove the Substring (easy version)

    D1.Remove the Substring (easy version) time limit per test2 seconds memory limit per test256 megabyt ...

  7. Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)

    D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...

  8. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】

    任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...

  9. E1. String Coloring (easy version)(贪心)

    E1. String Coloring (easy version) time limit per test 1 second memory limit per test 256 megabytes ...

随机推荐

  1. 关于R文件

    1 什么是R文件 R文件是自动生成的文件,里面保存的是res目录下所有资源的ID. 2 如何使用 2.1 在java代码中使用 txtName = (TextView)findViewById(R.i ...

  2. JSP页面读取数据中的数据内容,出现乱码现象的解决方法

    1.首先要确保JSP页面的编码已修改为“utf-8”的字符编码: 2.然后再在jsp页面上添加代码进行设置: 先用getBytes()方法读出数据,然后再new String()方法设置格式为“utf ...

  3. 关于Java多线程的一些面试问题

    1.ArrayList和Vecoter区别? Array和ArrayList的异同点一.Array和ArrayList的区别#1. Array类型的变量在声明的同时必须进行实例化(至少得初始化数组的大 ...

  4. ubuntu14 文件夹添加/删除书签

    1. 打开文件管理,进入你要添加书签的目录 2. 把鼠标移到顶部选择“Bookmarks" 3. 这是文件管理左侧可以看到 4. 右键可以选择删除

  5. 腾讯云从零搭建PHP运行环境

    一.首先我们得注册腾讯云,租用一台服务器,我选择的是CentOS 7.2 64位,这时候会给你这台主机的公网IP和内网IP,以及这台主机的用户名及密码. 二.我们可以使用腾讯云网页上自带的登录按钮进行 ...

  6. [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩)

    [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩) 题面 给出一棵树和一个图,点数均为n,问有多少种方法把树的节点标号,使得对于树上的任意两个节点u,v,若树上u ...

  7. 小白如何入门 Python 爬虫?

    本文针对初学者,我会用最简单的案例告诉你如何入门python爬虫! 想要入门Python 爬虫首先需要解决四个问题 熟悉python编程 了解HTML 了解网络爬虫的基本原理 学习使用python爬虫 ...

  8. python中输入某年某月某日,判断这一天是这一年的第几天?

    输入某年某月某日,判断这一天是这一年的第几天?程序分析 特殊情况,闰年时需考虑二月多加一天: 直接上代码 #定义一个函数,判断是否为闰年 def leapyear(y): return (y % 40 ...

  9. django部署到linux上不显示.svg图标处理方法

    在setting文件的最开始添加如下内容: import mimetypes mimetypes.add_type("image/svg+xml", ".svg" ...

  10. wordpress添加自定义菜单栏

    1. 以下代码是wordpress的wp_nav_menu(); <?php $defaults = array( 'container'=>false, //父级div为空 'link_ ...