Problem:
Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  long long  k;
  cin >> s >> k;
  long long ans = 0;
  long long size = s.size();
  
  if (size != 1){
  // 文字列の最初と最後が繋がってないやつ
  if (s.at(0) != s.at(size-1)){
    long long check = 0; // チェックするのが文字列sのどこか
    
    while(check < size-1){ // 最後までチェックしたら終了
      long long same = 1;
      for (long long i=check+1; i<size; i++){
        if (s.at(check) == s.at(i)){
          check++; same++;
        }
        else{
          check++; break;
        }
      }
      ans += same/2;
    }
    cout << ans * k <<endl;
  }
  
  /*
  // 文字列の最初から最後まで全部繋がってるやつ
  bool flagsuru=1;
  bool flagsimasu=1;
  while(flagsuru){
    for (int i=0; i<size-1; i++){
      if (s.at(i) != s.at(i+1)){
        flagsuru = 0;
        flagsimasu = 0;
        break;
      }
    }
  }
  
  if (flagsimasu) {
    int answer = size * k / 2;
    cout <<answer <<endl;
  }
  */
  
  // 文字列の最初と最後が繋がってるやつ
  else if (s.at(0) == s.at(size-1)){
    long long check = 0; // チェックするのが文字列sのどこか
    long long check2 = 0;
    long long check3 = size-1;
    long long same2 = 1;
    long long same3 = 1;
    long long same999 = 0;
    
    for (long long i=1; i<size; i++){
      if (s.at(check2) == s.at(i)){
        check2++; same2++;
      }
      else{
        check2++; break;
      }
    }
    for (long long i=size-2; i>=check2; i--){
      if (s.at(check3) == s.at(i)){
        check3--; same3++;
      }
      else{
        check3--; break;
      }
    }
    same999 = same2 + same3;
    // same2, same3：各々最初, 最後がどれだけ同じか
    // same999：最初と最後がどれだけ繋がってるか
    /* 0000000012312312312312312312300000000000
               ^                   ^
             check2              check3
       0000000000000000012000000000000000000000
                        ^^
                   check2check3
       0000000000000000001000000000000000000000
                         ^
                   check2, check3
    */
    
    check = check2; // 最初チェックした分は飛ばす
    while(check < check3){ // チェックした所までチェックしたら終了
      long long same = 1;
      for (long long i=check+1; i<check3+1; i++){
        if (s.at(check) == s.at(i)){
          check++; same++;
        }
        else{
          check++; break;
        }
      }
      ans += same/2;
    }
    /* ここまでで確認したこと
        1.文字列の最初, 最後がどれだけ繋がっているか
        2.文字列の中間のans
       やるべきこと
        1.最初、最後、最初＋最後を各々2で割り、各々1, 1, k-1を
          かけて答えに足す
        2.中間のansにkをかけて答えに足す
    */
    
    long long ans1 = same2 / 2 + same3 / 2 + same999 / 2 * (k-1);
    long long ans2 = ans * k;
    long long real_ans = ans1 + ans2;
    
    //cout << same2 / 2 <<" " << same3 / 2 <<" " <<same999 <<endl;
    //cout << ans1 <<endl;
    //cout << ans <<endl;
    cout << real_ans <<endl;
  }
  }
  else{
    cout << k/2 <<endl;
  }
}