http://codeforces.com/contest/950/problem/C
题意: 给长为n(<=2e5)由0,1组成的字符串,求分割成以0开头和结尾并且0,1交替出现的若干个字符串,要求它们的位置是递增的。
分析:
贪心, 赛时各种特判,因为写的代码 太烂,然后就卡在第八组数据。 改不动了,改完后最坏情况下复杂度成了O(n^2)
而大多数大佬 代码比较优美
使用vector记录下标,不能用普通数组, 爆内存
贪心思路:RT,以0开头,若有多个0连续,则分割数+1;若出现1,RT,放在0后面,此时检查是否还有以0结尾的字符串,若没有,输出-1,结束。
若有,加到0后面,分割数-1
见代码:
#include <bits/stdc++.h>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n)
#define pb push_back
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1.0);
const double E=2.718281828459045;
const double eps=1e-3;
const int INF=0x3f3f3f3f;
const int MOD=1e8+7;
const int N=2e5+5;
const ll maxn=1e6+5;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
vector<int>vec[N];
char s[N];
int main()
{
while(~scanf("%s",s))
{
rep(i,0,N) vec[i].clear();
int cnt=0,ans=0;
for(int i=0; s[i]; i++)
{
if(s[i]=='0')
vec[++cnt].pb(i+1);
else
{
if(cnt==0)
return 0*puts("-1");///cnt代表以0结尾的字符串的个数
vec[cnt--].pb(i+1);
}
ans=max(ans,cnt);
}
if(ans!=cnt)///满足此条件 说明有以'1'结尾的字符串
return 0*puts("-1");
printf("%d\n",ans);
for(int i=1; i<=ans; i++)
{
printf("%d",vec[i].size());
for(auto x:vec[i])
printf(" %d",x);
puts("");
}
}
return 0;
}