Educational Codeforces Round 110 (Rated for Div. 2) - C. Unstable String - DP

Posted Chivas_/Regal

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 110 (Rated for Div. 2) - C. Unstable String - DP相关的知识,希望对你有一定的参考价值。

题目:
在这里插入图片描述
思路:
因为’?'可以取0或1
所以这可以被看作一个决策问题
决策就可以联想到dp

在010101…后面如果是一个0
以0结尾的可选子串数量 = 上一步以1结尾的话可选字符串数量+1
此时以1结尾的字符串可选数量,就是0
后面是1同理
如果后面是’?'的话
那么这一位可以放0也可也放1
放0的话以0结尾的可选子串数量 = 上一步以1结尾的话可选字符串数量+1
放1的话以1结尾的可选子串数量 = 上一步以0结尾的话可选字符串数量+1

这样dp推导公式就有了
dp1表示这一位以1结尾的串构成的成立子串值
dp0表示这一位以0结尾的串构成的成立子串值

{ if(s[i] = ’0’) d p 0 [ i ] = d p 1 [ i − 1 ] + 1 , d p 1 [ i ] = 0 ; if(s[i] == ’1’)    d p 1 [ i ] = d p 0 [ i − 1 ] + 1 , d p 0 [ i ] = 0 ; if(s[i] == ’?’)    d p 1 [ i ] = d p 0 [ i − 1 ] + 1 , d p 0 [ i ] = d p 1 [ i − 1 ] + 1 ; \\begin{cases} & \\text{if(s[i] = '0')}\\qquad dp0[i] = dp1[i - 1] + 1, dp1[i] = 0; \\\\ & \\text{if(s[i] == '1')}\\quad\\;dp1[i] = dp0[i - 1] + 1, dp0[i] = 0;\\\\ & \\text{if(s[i] == '?')}\\quad\\;dp1[i] = dp0[i - 1] + 1, dp0[i] = dp1[i - 1] + 1; \\end{cases} if(s[i] = ’0’)dp0[i]=dp1[i1]+1,dp1[i]=0;if(s[i] == ’1’)dp1[i]=dp0[i1]+1,dp0[i]=0;if(s[i] == ’?’)dp1[i]=dp0[i1]+1,dp0[i]=dp1[i1]+1;
由于同一段[l, r]不能选两次
所以每次取MAX累加就行了

代码:

/*
           ________   _                                              ________                              _
          /  ______| | |                                            |   __   |                            | |
         /  /        | |                                            |  |__|  |                            | |
         |  |        | |___    _   _   _   ___  _   _____           |     ___|   ______   _____   ___  _  | |
         |  |        |  __ \\  |_| | | | | |  _\\| | | ____|          |  |\\  \\    |  __  | |  _  | |  _\\| | | |
         |  |        | |  \\ |  _  | | | | | | \\  | | \\___           |  | \\  \\   | |_/ _| | |_| | | | \\  | | |
         \\  \\______  | |  | | | | \\ |_| / | |_/  |  ___/ |          |  |  \\  \\  |    /_   \\__  | | |_/  | | |
Author :  \\________| |_|  |_| |_|  \\___/  |___/|_| |_____| _________|__|   \\__\\ |______|     | | |___/|_| |_|
                                                                                         ____| |
                                                                                         \\_____/
*/
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <utility>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#include <set>

#define G 10.0
#define LNF 1e18
#define EPS 1e-6
#define PI acos(-1.0)
#define INF 0x7FFFFFFF

#define ll long long
#define ull unsigned long long

#define LOWBIT(x) ((x) & (-x))
#define LOWBD(a, x) lower_bound(a.begin(), a.end(), x) - a.begin()
#define UPPBD(a, x) upper_bound(a.begin(), a.end(), x) - a.begin()
#define TEST(a) cout << "---------" << a << "---------" << '\\n'

#define CHIVAS_ int main()
#define _REGAL exit(0)

#define SP system("pause")
#define IOS ios::sync_with_stdio(false)
//#define map unordered_map

#define _int(a) int a; cin >> a
#define  _ll(a) ll a; cin >> a
#define _char(a) char a; cin >> a
#define _string(a) string a; cin >> a
#define _vectorInt(a, n) vector<int>a(n); cin >> a
#define _vectorLL(a, b) vector<ll>a(n); cin >> a

#define PB(x) push_back(x)
#define ALL(a) a.begin(),a.end()
#define MEM(a, b) memset(a, b, sizeof(a))
#define EACH_CASE(cass) for (cin >> cass; cass; cass--)

#define LS l, mid, rt << 1
#define RS mid + 1, r, rt << 1 | 1
#define GETMID (l + r) >> 1

using namespace std;

template<typename T> inline void Read(T &x){T f = 1; x = 0;char s = getchar();while(s < '0' || s > '9'){if(s == '-') f = -1; s = getchar();}while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}x*=f;}
template<typename T> inline T MAX(T a, T b){return a > b? a : b;}
template<typename T> inline T MIN(T a, T b){return a > b? b : a;}
template<typename T> inline void SWAP(T &a, T &b){T tp = a; a = b; b = tp;}
template<typename T> inline T GCD(T a, T b){return b > 0? GCD(b, a % b) : a;}
template<typename T> inline void ADD_TO_VEC_int(T &n, vector<T> &vec){vec.clear(); cin >> n; for(int i = 0; i < n; i ++){T x; cin >> x, vec.PB(x);}}
template<typename T> inline pair<T, T> MaxInVector_ll(vector<T> vec){T MaxVal = -LNF, MaxId = 0;for(int i = 0; i < (int)vec.size(); i ++) if(MaxVal < vec[i]) MaxVal = vec[i], MaxId = i; return {MaxVal, MaxId};}
template<typename T> inline pair<T, T> MinInVector_ll(vector<T> vec){T MinVal = LNF, MinId = 0;for(int i = 0; i < (int)vec.size(); i ++) if(MinVal > vec[i]) MinVal = vec[i], MinId = i; return {MinVal, MinId};}
template<typename T> inline pair<T, T> MaxInVector_int(vector<T> vec){T MaxVal = -INF, MaxId = 0;for(int i = 0; i < (int)vec.size(); i ++) if(MaxVal < vec[i]) MaxVal = vec[i], MaxId = i; return {MaxVal, MaxId};}
template<typename T> inline pair<T, T> MinInVector_int(vector<T> vec){T MinVal = INF, MinId = 0;for(int i = 0; i < (int)vec.size(); i ++) if(MinVal > vec[i]) MinVal = vec[i], MinId = i; return {MinVal, MinId};}
template<typename T> inline pair<map<T, T>, vector<T> > DIV(T n){T nn = n;map<T, T> cnt;vector<T> div;for(ll i = 2; i * i <= nn; i ++){while(n % i == 0){if(!cnt[i]) div.push_back(i);cnt[i] ++;n /= i;}}if(n != 1){if(!cnt[n]) div.push_back(n);cnt[n] ++;n /= n;}return {cnt, div};}
template<typename T>             vector<T>& operator--            (vector<T> &v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T> &v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T> &v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>  v){for (auto& i : v) os << i << ' '; return os;}


inline void solve(){
        string s; cin >> s;
        int n = s.size();
        ll res = 0;
        ll dp1[n + 10] = {0};
        ll dp0[n + 10] = {0};

        if(s[0] == '0') dp0[0] = 1;
        else if(s[0] == '1') dp1[0] = 1;
        else if(s[0] == '?') dp1[0] = dp0[0] = 1;

        for(int i = 1; i < n; i ++){
                if(s[i] == '0') dp0[i] = dp1[i - 1] + 1, dp1[i] = 0;
                else if(s[i] == '1') dp1[i] = dp0[i - 1] + 1, dp0[i] = 0;
                else dp1[i] = dp0[i - 1] 以上是关于Educational Codeforces Round 110 (Rated for Div. 2) - C. Unstable String - DP的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27