Codeforces Round #798 (Div. 2) D

Problem - C - Codeforces

动态规划

状态表示dp i,j: 对位置 i 上的字符,当 a 取状态 j 时的贡献值

状态转移:

当前a[i]!=?时,dp[i][a[i]]=dp[i-1][a[i]^1]+1

当前a[i]==?时,dp[i][a[i]]=max(dp[i-1][0],dp[i-1][1]);

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>

//#define int ll
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d",&x);
#define sl(x) scanf("%lld",&x);
#define ss(x) scanf("%s",x+1);

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 200010, M = 2 * N;
const int INF = 0x3f3f3f3f;
const long long LLINF = 1e18;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int n, m, k;
char s[N];
int dp[N][2];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll x, ll n, ll mod)
{
    ll res = 1;
    while (n)
    {
        if (n & 1)res = (1ll * res * x) % mod;
        x = (1ll * x * x) % mod;
        n >>= 1;
    }
    return res;
}

void init() {}

void solve()
{
    cin >> s + 1;
    n = strlen(s + 1);
    ll ans = 0;
    pre(i, 0, n)dp[i][0] = dp[i][1] = 0;;
    pre(i, 1, n)
    {
        if (s[i] == '0')dp[i][1] = dp[i - 1][0] + 1;
        else if (s[i] == '1') dp[i][0] = dp[i - 1][1] + 1;
        else
        {
            dp[i][1] = dp[i - 1][0] + 1;
            dp[i][0] = dp[i - 1][1] + 1;
        }
        ans += max(dp[i][0], dp[i][1]);
    }
    cout << ans << Endl;
    return;
}

signed main()
{
    int _;
    cin >> _;
    //_ = 1;
    init();
    while (_--)
    {
        solve();
    }
    return 0;
}

双指针

枚举子串末尾,对于一个区间[l,r]内,且以r为结尾的子字符串,其对整体答案的贡献值为r-l+1

eg: 10101

对于[1,4]区间内,且结尾为4的子字符串有0,10,010,1010(共4个)

枚举末尾,双指针去掉不合法的区域,累和即可得到答案

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>

//#define int ll
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d",&x);
#define sl(x) scanf("%lld",&x);
#define ss(x) scanf("%s",x);

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 200010, M = 2 * N;
const int INF = 0x3f3f3f3f;
const long long LLINF = 1e18;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int n, m, k;
char s[N];
int dp[N][2];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll x, ll n, ll mod)
{
    ll res = 1;
    while (n)
    {
        if (n & 1)res = (1ll * res * x) % mod;
        x = (1ll * x * x) % mod;
        n >>= 1;
    }
    return res;
}

void init() {}

void solve()
{
    string s;
    cin >> s;
    int ji_1 = 0, ji_0 = 0, ou_1 = 0, ou_0 = 0;
    int l = 0;
    ll ans = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == '0')
        {
            if (i % 2 == 0) ou_0++;
            else ji_0++;
        }
        else if (s[i] == '1')
        {
            if (i % 2 == 0) ou_1++;
            else ji_1++;
        }
        while ((ou_0 && ji_0) || (ji_1 && ou_1) || (ji_1 && ji_0) || (ou_1 && ou_0))
        {
            if (s[l] == '0')
            {
                if (l % 2 == 0) ou_0--;
                else ji_0--;
            }
            else if (s[l] == '1')
            {
                if (l % 2 == 0) ou_1--;
                else ji_1--;
            }
            l++;
        }
        // cout<<l<<" "<<i<<endl;
        ans += (1ll + i - l);
    }
    cout << ans << endl;
}

signed main()
{
    int _;
    cin >> _;
    //_ = 1;
    init();
    while (_--)
    {
        solve();
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值