题解:P3677 [CERC2016] 关键的膝盖 Key Knocking

· · 题解

考虑形如 "01010101" 的字符串,显然其权值为其长度。

易得若字符串 t 为 s 的字串,则 s 的权值大于等于 t 的权值。

将输入字符串三个三个分组,则恰分出 n 组,若每组中都包含一个 "01" 字串,则答案大于等与 2n。

长度为 3 的 01 字符串总共只有 8 种可能,手搓构造一下即可

000 -> 011

001 -> 001

010 -> 001

011 -> 011

100 -> 010

101 -> 011

110 -> 101

111 -> 001

没了。

Code:

#include <bits/stdc++.h>
using namespace std;
int n,lst;
char a[300005];
map<string,int> h;
signed main()
{
    h["000"]=1;h["001"]=-1;h["010"]=1;h["011"]=-1;h["100"]=0;h["101"]=0;h["110"]=1;h["111"]=0;
    scanf("%s",a+1);n=strlen(a+1);
    lst=-1;
    vector<int> ans;
    for(int i=1;i<=n;i+=3)
    {
        string nw;nw+=a[i];nw+=a[i+1];nw+=a[i+2];
        int opt=h[nw];
        if(opt!=-1) ans.push_back(i+opt);
    }
    printf("%d\n",(int)ans.size());
    for(auto p:ans) printf("%d ",p);
}