题意:给你一个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; }; }