よじろめ覚書

私の理解度重視のソースコードです。

ABC023B - 手芸王

問題:B - 手芸王

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main(void) {
    int n, ans = -1;
    string s, str = "b";

    scanf ("%d", &n);
    cin >> s;
    if (s == str) {
        ans = 0;
    } else {
        int k = 1;
        while (n >= str.length()) {
            if (k % 3 == 0) {
                str.insert(str.begin(), 'b');
                str.insert(str.end(), 'b');
            } else if (k % 3 == 1) {
                str.insert(str.begin(), 'a');
                str.insert(str.end(), 'c');
            } else {
                str.insert(str.begin(), 'c');
                str.insert(str.end(), 'a');
            }
    
            if (s == str) {
                ans = k;
                break;
            }

            k++;
        }
    }

    printf ("%d\n", ans);
    return 0;
}