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≤|S|≤100
- S consists of lowercase English letters.
- 1≤K≤109
- 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
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s,t,t1;
long long k;
cin >> s;
cin >> k;
int l = s.length();
t = s;
t1 = s;
long long ans1 = 0,ans2 = 0,ans3 = 0;
for(int i = 1; i < l; i++){
if(t[i] == t[i - 1]){
ans1++;
t[i] = '0';
}
}
if(s[0] != s[l - 1])
ans2 = ans1;
else{
if(s[0] == t[l - 1]){
s[0] = '0';
ans2++;
}
for(int i = 1; i < l; i++){
if(s[i] == s[i - 1]){
ans2++;
s[i] = '0';
}
}
}
if(t1[0] == s[l - 1]){
t1[0] = '0';
ans3++;
}
for(int i = 1; i < l; i++){
if(t1[i] == t1[i - 1]){
ans3++;
t1[i] = '0';
}
}
long long sum = ans1 + (ans3 + ans2) * ((k - 1) / 2);
if(k % 2 == 0)
sum += ans2;
cout << sum << endl;
return 0;
}