「ロジックサマナー」の「圧縮」


ランクCの問題なので本当はコードをここに書こうとは思っていなかった。
しかし、
改善の余地がかなりありそうなので残しておきたいと思い、ここに残すことにした。
特に、
GetCountOfContinuousCharメソッドの中でrefを使ったことになんとなく違和感を感じた。
あと、このメソッドの命名もわかりにくいと感じた。
後で直せたらいいなと思う。

いかにコードを記載しますが、
この問題のヒントを求めに来た方で、まだ回答を見たくないという方は見ないでください。
ヒントではなくC#による回答になっています。

compression.cs
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        //-文字列を受け取る
        string S = Console.ReadLine();

        //-文字列の中から連続したb,wの数をカウントして、List<int> resultに格納する
        int currentSIndex = 0;
        List<int> result = new List<int>();
        while (currentSIndex < S.Length)
        {
            int CountOfContinuousChar = GetCountOfContinuousChar(S, ref currentSIndex, 'w');
            if (CountOfContinuousChar != 0) result.Add(CountOfContinuousChar);
            CountOfContinuousChar = GetCountOfContinuousChar(S, ref currentSIndex, 'b');
            if (CountOfContinuousChar != 0) result.Add(CountOfContinuousChar);
        }

        if (S[0] == 'w') result.Insert(0, 0);

        //-List<int> result内の要素を半角スペース区切りで出力する
        int count = 0;
        foreach (int r in result)
        {
            count++;
            Console.Write(r);
            if (count != result.Count) { Console.Write(" "); }
            else { }
        }
        Console.WriteLine();
        //Console.ReadLine();
    }
    static int GetCountOfContinuousChar(string S, ref int currentSIndex, char blackOrWhite)
    {
        List<int> result = new List<int>();
        int count = 0;
        while (currentSIndex < S.Length)
        {
            if (S[currentSIndex] == blackOrWhite)
            {
                count++;
                currentSIndex++;
            }
            else
            {
                break;
            }
        }

        return count;
    }
}