CodeForces - 950C Zebras 模拟变脑洞的天秀代码

题意:给你一个01串,问其是否能拆成若干形如0101010的子串,若能,输出所有子串的0,1 的位置。

题解:一开是暴力,然后瞎找规律,

最后找到一种神奇的线性构造法:扫一遍字符串,若为0就一直竖着往下写0,碰到1就回头往上写,再碰到0 就回头往下写······判断无法构造的依据:如果写1写得超过了上界就跳出,如果最后写的0不在最下面也跳出//codeforces上看到的一段代码秀的脑壳疼

坑:之前随便找规律,写了个巨丑的代码,逻辑混乱,直接wa掉。

无脑写的直接T了

ac

#define _CRT_SECURE_NO_WARNINGS
 #include <iostream>
 #include <cstdio>
#include <algorithm>
#include <string.h>
#include<string>
#include<vector>
#include<queue>
#define pb push_back
 #define _for(i, a, b) for (int i = (a); i<(b); ++i)
 #define _rep(i, a, b) for (int i = (a); i <= (b); ++i
        using namespace std;
        const int maxn = 200000 + 5;
        string s;

        vector<int>ans[maxn];
        int main() {
            cin >> s;
            
            int j = 0, x=0;
                _for(i, 0, s.size()) {
                if (s[i] - 48) { if (!j)return cout << "-1", 0; ans[--j].pb(i + 1); }else { ans[j++].pb(i + 1); }x = max(x, j);}
            if (x != j)return cout << "-1", 0;
            cout <<x << endl;_for(i,0,x){cout << ans[i].size() ; _for(j,0,ans[i].size())cout << ' ' << ans[i][j]; cout << endl;}

        }

附上天秀代码:

#include <bits/stdc++.h>
#define pb push_back
#define fi(x,n) for(int i=0;i<n;i++)
#define fj(x,n) for(int j=0;j<n;j++)
using namespace std;
vector <int> a[200009];
string s; int z, o, j, x, t, c;
int main() {
    cin >> s; fi(0, s.size()) { s[i] -= 48; if (s[i]) { if (!j)return cout << "-1", 0; a[--j].pb(i + 1); } else a[j++].pb(i + 1); x = max(x, j); }
    if (x != j)return cout << "-1", 0;
    cout << x << endl; fi(0, x) { cout << a[i].size() << " "; fj(0, a[i].size())cout << a[i][j] << " "; cout << endl; };
}

 

转载于:https://www.cnblogs.com/SuuT/p/8619227.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值