D1. Kirk and a Binary String (easy version)

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[2][maxn];
ll dp[2];
void LIS(string s,int st,int  val[])
{
    dp[0]=dp[1]=0;
    for(int i=st;i<N;i++){
        if(s[i]==‘0‘){
            dp[0]++;
        }else dp[1]=max(dp[0],dp[1])+1;
        val[i]=max(dp[0],dp[1]);
    }

}
signed main()
{
    string s,t;
    cin>>s;
    N=s.size();
    t=s;
    //int len=0;
    for(int i=N-1; i>=0;i--)
    {
        if(s[i]==‘1‘)
        {
            t[i]=‘0‘;
            LIS(s,i,val[0]);
            LIS(t,i,val[1]);
            for(int j=i;j<N;j++){
                if(val[0][j]!=val[1][j]){
                    t[i]=‘1‘;
                    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[2][maxn];
ll dp[2];
void LIS(string s,int st,int  val[])
{
    dp[0]=dp[1]=0;
    for(int i=st;i<N;i++){
        if(s[i]==‘0‘){
            dp[0]++;
        }else dp[1]=max(dp[0],dp[1])+1;
        val[i]=max(dp[0],dp[1]);
    }

}
signed main()
{
    string s,t;
    cin>>s;
    N=s.size();
    t=s;
    int cnt=0;
    for(int i=N-1; i>=0;i--)
    {
       if(s[i]==‘0‘){
         cnt++;
       }else if(cnt==0&&s[i]==‘1‘){
           t[i]=‘0‘;
       }else cnt--;
    }
    cout<<t<<‘\n‘;

}

原文地址:https://www.cnblogs.com/liulex/p/11386740.html

时间: 2024-10-09 08:57:51

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

Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)

Codeforces Round #575 (Div. 3) D1 - RGB Substring (easy version) The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given a

05-2. Saving James Bond - Easy Version (25)

05-2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured

pat05-图2. Saving James Bond - Easy Version (25)

05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was capture

2016 省热身赛 Earthstone: Easy Version

Earthstone: Easy Version Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Earthstone is a famous online card game created by Lizard Entertainment. It is a collectible card game that revolves around turn-based mat

05-图2. Saving James Bond - Easy Version (25)

05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was capture

NYOJ 5 Binary String Matching【string find的运用】

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

NYOJ5 Binary String Matching

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘100111011

NYOJ 5 Binary String Matching (kmp 字符串匹配)

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

NYOJ 5 Binary String Matching

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘100111011