Codeforces A. One-dimensional Japanese Crossword

//Next Codeforces Round #354 (Div. 2)
#include<bits/stdc++.h>

//#include<cstdio>
//#include<iostream>
//#include<algorithm>
//#include<vector>
//#include<cstring>
//#include<cmath>
//#include<map>

using namespace std;

#define fast ios_base::sync_with_stdio(false)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")

typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef map<char , bool>mpbc;

int main()
{
    fast;
    int n , cnt=0 , i , len=0 , vlen=0 , group=0;
    string s;
    vii v;
    mpbc mp;

    while(cin >> n)
    {
        v.clear();
        mp.clear();

        cin >> s;

        len = s.length();

        cnt=0;
        group=0;
        char ch='B';

        for(i=0; i<len+1; i++)
        {
            if(s[i] == 'B')
            {
                if(!mp[s[i]])
                {
                    cnt++;
                    group++;

                    mp[s[i]] = true;
                }

                else
                {
                    cnt++;
                }
            }

            else if(s[i] == 'W' or s[i]==NULL)
            {
                mp[ch] = false;

                if(cnt > 0)
                {
                    v.push_back(cnt);
                }

                cnt=0;
            }
        }

        vlen = v.size();

        if(!group)
        {
            outn("0");
            continue;
        }

        bool f=false;

        outn(group);

        for(i=0; i<vlen; i++)
        {
            if(!f)
            {
                cout << v[i];
                f=true;
            }

            else
            {
                cout << " " << v[i];
            }
        }

        cout << "\n";
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number