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 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
	long int i, j;
	long int n, k;
	long int count = 1;
	long int saigo = 0;
	long int saisyo = 0;
	long int sum = 0;
	long int ans = 0;
	char work;
	char str[200];
	
	scanf("%s%ld", str, &k);
	
	n = strlen(str);
	
	str[n] = '0';
	
	//printf("%ld %ld %s %ld\n", strlen(str), k, str, n);
	
	work = str[0];
	
	if(str[0] == str[n - 1]) {
		count = 1;
		for(i = n - 2; i > 0; i--) {
			if(work != str[i]) {
				break;
			}
			count++;
		}
		saigo = count;
		
		if(saigo == n - 1) {//全部同じ文字の場合
			ans = n * k / 2;
			printf("%ld\n", ans);
			return 0;
		} else {//最初と最後は同じ文字だけど間に必ず違う文字が入る場合
			count = 1;
			work = str[0];
			for(i = 1; i < n; i++) {
				if(work == str[i]) {
					count++;
				} else {
					break;
				}
			}
			
			saisyo = count;//最初の文字から同じ文字が何文字続いてるか
			count = 1;
			work = str[saisyo];
			
			sum = 0;
			//間の文字の連続具合
			for(i = saisyo + 1; i < n - saigo + 1; i++) {
				if(work == str[i]) {
					count++;
				} else {
					sum += count / 2;
					count = 1;
					work = str[i];
				}
			}
			
			ans = sum * k;
			ans +=  ( (saisyo + saigo) / 2 ) * (k - 1);
			ans += (saisyo / 2);
			ans += (saigo / 2);
			printf("%ld\n", ans);
			return 0;
		}
	} else {//最初と最後の文字が違う場合間の文字の連続具合を見ればいい
		sum = 0;
		count = 1;
		work = str[0];
		for(i = 1; i <= n; i++) {
			if(work == str[i]) {
				count++;
			} else {
				sum += count / 2;
				count = 1;
				work = str[i];
			}
		}
		ans = sum * k;
		printf("%ld\n", ans);
		return 0;
	}
	
	printf("%ld\n", ans);
	
	return 0;
}