One-dimensional Japanese puzzle I have been practicing competitive coding for a while now and am using codeforces.com. I recently encountered a problem called "One Dimensional Japanese Crossword" on that site. 
The problem is as follows:

The judge provides an integer (up to 100) and then a string with that many characters being either 'B' or 'W'. We need to output how many groups of 'B' there are and how many 'B's there are in each group. For example:

Input:

6
BBWBWB


Output:

3
2 1 1




After lots of tries and lots of fails, I was finally able to get the code accepted by the judge. However, for some reason I feel that my code is really inefficient and there might be an easier and better way to solve this problem.

#include&lt;iostream&gt;
#include&lt;vector&gt;
using namespace std;

int main()
{
    int n;
    cin &gt;&gt; n;
    string s;
    cin &gt;&gt; s;

    int bGroups = 0;
    int wGroups = 0;

    char initChar = s[0];

    for (int i = 1; i &lt; s.length(); i++)
    {
        if (initChar == 'B')
        {
            if (s[i] != initChar)
            {
                bGroups++;
                initChar = 'W';
            }
        }
        else if (initChar == 'W')
        {
            if (s[i] != initChar)
            {
                wGroups++;
                initChar = 'B';
            }
        }

    }

    if (s[n - 1] == 'B')
    {
        bGroups++;
    }
    else if (s[n - 1] == 'W')
    {
        wGroups++;
    }

    char init = s[0];
    vector&lt;int&gt;grpSize(bGroups);
    int counter = 0;
    int i = 0;
    while (counter &lt; bGroups)
    {
        if (init == 'B')
        {
            while (init == 'B')
            {
                grpSize[counter]++;
                i++;
                init = s[i];
            }
            counter++;
            while (init == 'W')
            {
                i++;
                init = s[i];
            }
        }
        else
        {
            while (init == 'W')
            {
                i++;
                init = s[i];
            }
            while (init == 'B')
            {
                grpSize[counter]++;
                i++;
                init = s[i];
            }
            counter++;
        }
    }

    cout &lt;&lt; bGroups &lt;&lt; endl;
    for (int i = 0; i &lt; bGroups; i++)
    {
        cout &lt;&lt; grpSize[i] &lt;&lt; " ";
    }
}


So basically, in the first pass over the string, I count how many groups of 'B' and 'W' there are. Then I create an int vector to store the size of each 'B' group. Then on the second pass I fill in the values into the vector and then output the results.
Code with iterators and standard algorithms, and you'll see your coding style improve dramatically:


finding the beginning of a group of Bs can be done with std::find: auto b = std::find(first, last, 'B');
finding the end of a group of Bs can also be done with std::find: auto e = std::find(b, last, 'W');
computing the distance between the two is the job of std::distance: auto nb_bs = std::distance(b, e).


So combining all that we get:

#include &lt;vector&gt;
#include &lt;algorithm&gt;

template &lt;typename Iterator&gt;
auto groups_of_bs(Iterator first, Iterator last) {
    std::vector&lt;int&gt; result;
    while (true) {
        first = std::find(first, last, 'B');
        if (first == last) break;
        auto w = std::find(std::next(first), last, 'W');
        result.push_back(std::distance(first, w));
        if (w == last) break;
        first = std::next(w);
    }
    return result;
}


Now the only thing left is to display the size of the vector, and then its elements:

#include &lt;string&gt;
#include &lt;iostream&gt;

int main() {
    std::string test{"BBWBWB"};
    auto res = groups_of_bs(test.begin(), test.end());
    std::cout &lt;&lt; res.size() &lt;&lt; '\n';
    for (auto i : res) std::cout &lt;&lt; i &lt;&lt; ' ';
}