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



Sample input:
issii
2


Sample output:
4

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main() {

	string S;
	cin >> S;

	ll k;
	cin >> k;

	ll lng = S.size();

	int flag = 0;
	for (string::size_type i = 1; i < S.size(); i++) {
		if (S[i] != S[i - 1]) {
			flag = 1;
		}
	}

	vector<ll>cnts;
	ll cnt = 1;
	for (string::size_type i = 1; i < S.size(); i++) {
		if (S[i] == S[i - 1]){
			cnt++;
		}
		else {
			cnts.push_back(cnt);
			cnt = 1;
		}
	}

	cnts.push_back(cnt);

	ll ans = 0;
	if (S.size() == 1) {
		ans = k / 2;
		cout << ans << endl;
	}
	else if (flag == 0) {
		ans = lng / 2;
		cout << ans << endl;
	}
	else {

		for (vector<int>::size_type i = 1; i < cnts.size() - 1; i++) {
			if (cnts[i] > 1) {
				ll b = cnts[i] / 2;
				ans += k * b;
			}
		}

		//cout << ans << endl;

		if (k > 1) {
			ll a = (cnts[0] + cnts[cnts.size()-1]) / 2;
			cout << cnts[0]/2 + ans + a*(k-1) << endl;
		}
		else {
			cout << ans << endl;
		}
	}



	return 0;
	

}

Submission status: Runtime Error

Solution code:
#文字列wordの文字の種類の構成の表現のためのリストを返す関数
def connection_count(word)->list:
    i=0
    a=[]
    count=1
    while i<(len(word)-1):
        while word[i]==word[i+1]:
            count+=1
            i+=1
            if i>=(len(word)-1):
                break
        a.append(count)
        count=1
        i+=1
    if a[-1]==1:
        a.append(count)
    return a

#S:文字列、K：繰り返す回数
S=input()
K=int(input())

#Sの文字の種類の構成を表現するためのリスト
connection=connection_count(S)

#結果の表示

#すべての文字が同じ場合
if len(connection)==1:
    print(((connection[0]*K)//2))
#最初と最後の文字が異なる場合
elif S[0]!=S[-1]:
    connection2=list(map(lambda x:x//2,connection))
    print((sum(connection2)*K))
#最初と最後の文字が同じ場合（すべての文字が同じ場合を除く）
else:
    t=S*2
    connection2=list(map(lambda x:x//2,connection))
    connection3=list(connection_count(t))
    connection4=map(lambda x:x//2,connection3)
    a=sum(connection2)
    b=sum(connection4)
    ans=a+(b-a)*(K-1)
    print(ans)

Submission status: Runtime Error

Solution code:
mycnt = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        mycnt += 1

if len(set(S)) == 1:
    print(len(S)*K//2)

elif S[0] != S[-1]:
    
    print(mycnt * K)

else:
    myleft = 1
    myright = 0
    mys = S[0]
    i = 1
    while S[i] == mys:
        myleft += 1
        i += 1
    
    myright = 0
    i = -1
    while S[i] == mys:
        myright += 1
        i -= 1
    print( (mycnt * K) - ( myleft//2 + myright//2 - (myright + myleft)//2 ) * ( K - 1 ) )

Submission status: Runtime Error

Solution code:
from collections import Counter
def main():
   s = input()
   k = int(input())
   li = []
   num = 1
   for i in range(len(s)-1):
       if s[i] != s[i+1]:
           li.append(1)
       else:
           li[-1] += 1
   temp = 0
   ans = 0
   if len(list((Counter(li).keys()))) == 1:
       ans += len(s) * k // 2
   else:
       for i in li:
           temp += i//2
       if s[0] != s[-1]:
           ans = temp * k
       else:
           li[0] += li[-1]
           for i in li[:-1]:
               ans += i//2
           ans = ans * (k-1) + temp
   print(ans)
main()

Submission status: Runtime Error

Solution code:
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1:
        cnt = (len(a)*K)//2
        print(cnt)
    else:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        
        if a[0] == a[-1]:
            temp_3 = (temp + temp_2)//2
            cnt += temp_3 * (K-1)
        temp_4 = 0
        elif temp > 1:
            temp_4 += temp
        elif temp_2 > 2:
            temp_4 += temp_2
        cnt += temp_4 * (K-1)


        print(cnt)

    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby

# 入力 =stdin.readline().rstrip()
# 分割 = stdin.readline().rstrip().split()

'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''

'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''

'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  # 和集合
print(list(set(a) & set(b)))  # 積集合
'''

'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''

'''
.append(要素)
.insert(場所,要素)
-1で末尾で末尾から一つ前
'''
'''
間に空白追加
' '.join(lis)
'''
T = stdin.readline().rstrip()
num = int(stdin.readline().rstrip())
midcount = 1
res = []
ch = ''
for t in range(len(T)):
    if (ch == T[t] and t != 0):
        midcount += 1
    elif (ch != T[t] and t != 0):
        res.append(midcount)
        midcount = 1
    if (ch == T[t] and t == len(T) - 1):
        res.append(midcount)
    ch = T[t]

if (len(res) == 1):
    print(str(int(num * len(T) / 2)))
elif (T[0] == T[len(T) - 1]):
    counter = 0
    tes = []
    tes.append(res[0] + res[len(res) - 1])
    tes += res[1: len(res) - 1]
    for x in tes:
        counter += int(x / 2)
    counter *= num - 1
    for x in res:
        counter += int(x / 2)
    print(counter)
else:
    counter = 0
    for x in res:
        counter += int(x / 2)
    counter *= num
    print(counter)

Submission status: Runtime Error

Solution code:
from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import copy

# 入力 =stdin.readline().rstrip()
# 分割 = stdin.readline().rstrip().split()

'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''

'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''

'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  # 和集合
print(list(set(a) & set(b)))  # 積集合
'''

'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''

'''
.append(要素)
.insert(場所,要素)
-1で末尾で末尾から一つ前
'''
'''
間に空白追加
' '.join(lis)
'''
T = stdin.readline().rstrip()
num = int(stdin.readline().rstrip())
midcount = 1
res = []
ch = ''
for t in range(len(T)):
    if (ch == T[t] and t != 0):
        midcount += 1
    elif (ch != T[t] and t != 0):
        res.append(midcount)
        midcount = 1
    if (ch == T[t] and t == len(T) - 1):
        res.append(midcount)
    ch = T[t]

if (len(res) == 1):
    print(str(int(num * len(T) / 2)))
elif (T[0] == T[len(T) - 1]):
    counter = 0
    tes = []
    tes.append(res[0] + res[len(res) - 1])
    tes += res[1: len(res) - 1]
    for x in tes:
        counter += int(x / 2)
    counter *= num - 1
    for x in res:
        counter += int(x / 2)
    print(counter)
else:
    counter = 0
    for x in res:
        counter += int(x / 2)
    counter *= num
    print(counter)

Submission status: Runtime Error

Solution code:
from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import copy

# 入力 =stdin.readline().rstrip()
# 分割 = stdin.readline().rstrip().split()

'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''

'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''

'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  # 和集合
print(list(set(a) & set(b)))  # 積集合
'''

'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''

'''
.append(要素)
.insert(場所,要素)
-1で末尾で末尾から一つ前
'''
'''
間に空白追加
' '.join(lis)
'''
T = stdin.readline().rstrip()
num = int(stdin.readline().rstrip())
midcount = 1
res = []
ch = ''
for t in range(len(T)):
    if (ch == T[t] and t != 0):
        midcount += 1
    elif (ch != T[t] and t != 0):
        res.append(midcount)
        midcount = 1
    if (ch == T[t] and t == len(T) - 1):
        res.append(midcount)
    ch = T[t]

print(res)
if (len(res) == 1):
    print(str(int(num * len(T) / 2)))
elif (T[0] == T[len(T) - 1]):
    counter = 0
    tes = []
    tes.append(res[0] + res[len(res) - 1])
    tes += res[1: len(res) - 1]
    for x in tes:
        counter += int(x / 2)
    counter *= num - 1
    for x in res:
        counter += int(x / 2)
    print(counter)
else:
    counter = 0
    for x in res:
        counter += int(x / 2)
    counter *= num
    print(counter)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
ans = 0
flag=0
if S[0]==S[-2]:
  if S[-2]!=S[-3]:
    	flag = 1
  ans+=1
  S[-2]="aaaa"
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if flag==0:
	print(ans*N)
else:
  print(ans*N-1)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
t = ''
i = 0
count = 0
start = 0
end = 0

for i in range(len(s)-1):
	if s[i] == s[i+1]:
		count +=1

if count == len(s)-1:#すべて同じ場合
	print(count/2*k)

elif s[0] != s[len(s)-1]:#先頭と末尾が異なる場合
	print(count/2*k)

elif s[0] == s[len(s)-1]#先頭と末尾が同じ場合
	for i in range(len(s)-1):
		if s[0] == s[i+1]: 
			start += 1
		else:
			break
	for i in range(len(s)-1,0):
		if s[len(s)-1] == s[i-1]:
			end += 1
		else:
			break
	print(count*k-(start+end)/2)

Submission status: Runtime Error

Solution code:
import sys, collections
 
def solve():
    N = int(input())
    edge = dict()
    col = dict()
    for i in range(N): 
        edge[i] = set()
        col[i] = -1
        s = input()
        for j in range(N):
            if s[j] == "1": edge[i] |= {j}
 
    colq = collections.deque()
    colq.append((0, 0))
    while colq:
        now_node, now_col = colq.popleft()
        if col[now_node] == -1:
            col[now_node] = now_col
            for next_node in edge[now_col]: colq.append((next_node, 1 ^ now_col))
        else:
            if col[now_node] != now_col: 
                print(-1)
                break
    else:
        dist = dict()
        for i in range(N):
            dist[i] = dict()
            for j in range(N):
                if j in edge[i]:
                    dist[i][j] = 1
                    dist[j][i] = 1
                elif i == j: dist[i][j] = 0
                else: dist[i][j] = 1000000000
 
        for k in range(N):
            for i in range(N):
                for j in range(N):
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
 
        ans = 0
        for i in range(N):
            for j in range(N):
                if dist[i][j] < 1000000000: ans = max(ans, dist[i][j])
        print(ans + 1)
 
    return 0
 
if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
import copy

S = list(input())
K = int(input())

def Scount():
    cnt_S = 0
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            S[i+1] = "1"
            cnt_S += 1
    return cnt_S

if K == 1:
    ans = Scout()

else:
    T = copy.copy(S)
    T.insert(0, T[len(T)-1])
    cnt_T = 0
    for i in range(len(T)-1):
        if T[i] == T[i+1]:
            T[i+1] = "1"
            cnt_T += 1

    if T[i+1] != "1":
        ans = Scount() + (cnt_T * (K-1))
    else:
        TT = S * 2
        cnt_T = 0
        for i in range(len(TT)-1):
            if TT[i] == TT[i+1]:
                TT[i+1] = "1"
                cnt_T += 1
        if K % 2 == 0:
            ans = cnt_T * K / 2
        else:
            ans = (cnt_T * K // 2) + Scount()

print(ans)

Submission status: Runtime Error

Solution code:
import sys, collections
 
def solve():
    N = int(input())
    edge = dict()
    col = dict()
    for i in range(N): 
        edge[i] = set()
        col[i] = -1
        s = input()
        for j in range(N):
            if s[j] == "1": edge[i] |= {j}
 
    colq = collections.deque()
    colq.append((0, 0))
    while colq:
        now_node, now_col = colq.popleft()
        if col[now_node] == -1:
            col[now_node] = now_col
            for next_node in edge[now_col]: colq.append((next_node, 1 ^ now_col))
        else:
            if col[now_node] != now_col: 
                print(-1)
                break
    else:
        dist = dict()
        for i in range(N):
            dist[i] = dict()
            for j in range(N):
                if j in edge[i]: dist[i][j] = 1
                elif i == j: dist[i][i] = 0
                else: dist[i][j] = 1000000000
 
        for k in range(N):
            for i in range(N):
                for j in range(N):
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
 
        ans = 0
        for i in range(N):
            for j in range(N):
                if dist[i][j] < 1000000000: ans = max(ans, dist[i][j])
        print(ans + 1)
 
    return 0
 
if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
S = list(input())
K = int(input())

a_li = []
b = len(S)
i = 0
j = 0
S.append(0)
x = 0

while i != b:
    a = 1
    while S[j] == S[j+1]:
        i += 1
        j += 1
        a += 1
    i += 1
    j += 1
    a_li.append(a)

if S[0] == S[-2]:
    if a_li[0]%2 == 1:
        if a_li[-2]%2 == 1:
            x = K-1
scr = 0
for n in range(len(a_li)):
    scr += a_li[n] // 2

if b == 1:
    scr = K//2
    print(scr)
else:
    print(scr*K+x)

Submission status: Runtime Error

Solution code:
import queue
import numpy as np
n = int(input())
class Node:
    def __init__(self):
        self.nextNode = []
        self.checkNgo = -1
        self.group = -1
        
Nodes = [Node() for i in range(n)]
for i in range(n):
    s = input()
    for j in range(len(s)):
        if s[j] == '1':
            Nodes[i].nextNode.append(j)
            
def check(node,ngo):
    if node.checkNgo == -1:
        node.checkNgo = ngo
    elif node.checkNgo != ngo:
        return False
    elif node.checkNgo == ngo:
        return True
    for i in node.nextNode:
        if not check(Nodes[i],(ngo+1)%2):
            return False
    return True
    
def ngoCheck():
    stack = [Nodes[0]]
    Nodes[0].checkNgo = 0
    while stack:
        node = stack.pop()
        for i in node.nextNode:
            if Nodes[i].checkNgo != -1 and Nodes[i].checkNgo == node.checkNgo:
                return False
            if Nodes[i].checkNgo == -1:
                Nodes[i].checkNgo = (node.checkNgo+1)%2
                stack.append(Nodes[i])
    return True
    
if not ngoCheck():
    print(-1)
    exit(0)
    
def solv():
    d = [[0 for i in range(n)] for j in range(n)]
    for i in range(n):
        for j in range(n):
            if j in Nodes[i].nextNode:
                d[i][j] = 1
            else:
                d[i][j] = np.inf
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][j] > d[i][k]+d[k][j]:
                    d[i][j] = d[i][k]+d[k][j]
    return d
    
def saidai(d):
    maxi = 0
    for i in range(n):
        for j in range(n):
            maxi = max(maxi,d[i][j])
    return maxi
    
d = solv()
print(saidai(d)+1)

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())

buf=''
cnt = 1
total = 0
cnts = []

for s in S:
    #print(buf,s,cnt)
    if s == buf:
        cnt += 1       
    else:
        cnts.append(cnt)
        total += int(cnt/2)
        cnt = 1        
    buf = s
cnts.append(cnt)    
total += int(cnt/2)

if len(set(list(S)))==1:
    print(int(len(S*K)/2))
elif S[0]==S[-1] :#and cnts[1] % 2 != 0:
    print(int(total*K - ((int(cnts[1]/2)+int(cnts[-1]/2)-((cnts[1]+cnts[-1])/2))*(K-1))))
else:
    print(total*K)

Submission status: Runtime Error

Solution code:
def zen(t):
    aa=0
    for i in range(len(t)-1):
        if t[i]==t[i+1]:
            aa+=1
        if len(t)-1==aa:
            return True
        else:
            return False

s=input()
k=int(input())
cnt=[]
ans=0
c=1
if zen(s):
    print(len(s)*k//2)
else:
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            c+=1
        else:
            cnt.append(c)
            c=1
        if i==len(s)-2:
            cnt.append(c)
    ans=0
    if s[0]==s[-1]:
        for i in range(len(cnt)):
            ans+=cnt[i]//2
        print(ans*k+(k-1)-(k-1)*(cnt[0]//2 + cnt[-1]//2 - (cnt[0]+cnt[-1])//2))
    else:
        for i in range(len(cnt)):
            ans+=cnt[i]//2
        print(ans*k)

Submission status: Runtime Error

Solution code:
def zen(t):
    aa=0
    for i in range(len(t)-1):
        if t[i]==t[i+1]:
            aa+=1
        if len(t)-1==aa:
            return True
        else:
            return False

s=input()
k=int(input())
cnt=[]
ans=0
c=1
if zen(s):
    print(len(s)*k//2)
else:
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            c+=1
        else:
            cnt.append(c)
            c=1
        if i==len(s)-2:
            cnt.append(c)
    ans=0
    if s[0]==s[-1]:
        for i in range(len(cnt)):
            ans+=cnt[i]//2
        print(ans*k+(k-1)-(cnt[0]//2 + cnt[-1]//2 - (cnt[0]+cnt[-1])//2))
    else:
        for i in range(len(cnt)):
            ans+=cnt[i]//2
        print(ans*k)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
chain_hajime=0
chian_owa=0
for i in range(len(s)):
    if s[0]==s[i]:
        chain_hajime+=1
    else: break
for i in range(len(s)):
    if s[-1]==s[-(i+1)]:
        chian_owa+=1
    else: break
print(chain_hajime)
print(chain_owa)
import re
def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """

    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
a=list(nchars(s, 2))
b=0
for i in a:
    b+=len(i)//2


if len(set(s))==1:
    print(len(s)*k//2)
elif s[0]!=s[-1]:
    print(b*k)
else:
    print(b*k-(chain_hajime//2+chain_owa//2-(chain_hajime+chain_owa)//2)*(k-1))

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())

buf=''
cnt = 1
total = 0
cnts = []


for s in S:
    if s == buf:
        cnt += 1       
    else:
        cnts.append(cnt)
        total += int(cnt/2)
        cnt = 1        
    buf = s
cnts.append(cnt)    
total += int(cnt/2)

if len(set(list(S)))==1:
    print(int(len(S*K)/2))

elif S[0]==S[-1] :#and cnts[1] % 2 != 0:
    print(int(total*K - (int(cnts[1]/2)+int(cnts[-1]/2)-((cnts[1]+cnts[-1])/2))*(K-1)))
else:
    print(total*K)

Submission status: Runtime Error

Solution code:
n = int(input())
x = int(input(), 2)

print(x * (x+1))

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
m=0
j=0
if len(set(list(s)))==1:print((len(s)*k)//2);exit()
for i in range(len(s)):
  if len(s)<=j+1:
    a=m*k
    if s[0]==s[-1]==s[1]==s[len(s)//3]:print(a+k-1)
    else:print(a)
  if s[j]==s[j+1]:
    m+=1
    j+=1
  j+=1

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
m=0
j=0
if len(set(list(s)))==1:print((len(s)*k)//2);exit()
for i in range(len(s)):
  if len(s)<=j+1:
    a=m*k
    if s[0]==s[-1]==s[len(s)//3]:print(a+k-1)
    else:print(a)
  if s[j]==s[j+1]:
    m+=1
    j+=1
  j+=1

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
m=0
j=0
if len(set(list(s)))==1:print((len(s)*k)//2);exit()
for i in range(len(s)):
 if len(s)<=j+1:
  a=m*k
  if s[0]==s[-1]==s[len(s)//3]:print(a+k-1)
  else:print(a)
 if s[j]==s[j+1]:
  m+=1
  j+=1
 j+=1

Submission status: Runtime Error

Solution code:
import numpy as np
S = list(input())
K=int(input())
count1=[]
count2=[]
count=0
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        count+=1
        if i==len(S)-2:
            count1.append(count)
            count=0
    elif count!=0 and S[i]!=S[i+1]:
        count1.append(count)
        count=0
Num1=len(count1)   
count1=np.array(count1)

for i in range(len(S)-1):
    if S[len(S)-1]==S[len(S)-i-2]:
        count+=1
        if i==len(S)-2:
            count2.append(count)
            count=0
    elif  S[len(S)-1]!=S[len(S)-i-2]:
        count2.append(count)
        count=0
        break

for i in range(len(S)-1):
    if S[len(S)-1]==S[i]:
        count+=1
        if i==len(S)-2:
            count2.append(count)
            count=0
    elif  S[len(S)-1]!=S[i]:
        count2.append(count)
        count=0
        break
a1=int(np.sum((count1+1)//2))
if count2[1]!=0:
    if count2[0]==len(S)-1:
        print(len(S)*K//2)
    elif count2[0]>=1 and count2[1]==1:
        print(a1*K+((sum(count2)+1)//2-(count1[Num1-1]+1)//2)*(K-1))
    elif count2[0]>=1 and count2[1]>1:
        print(a1*K+((sum(count2)+1)//2-(count1[Num1-1]+1)//2-(count1[0]+1)//2)*(K-1))
    elif count2[0]==0 and count2[1]==1:
        print(np.sum((count1+1)//2)*K+(K-1))
    elif count2[0]==0 and count2[1]>=1:
        print(a1*K+((sum(count2)+1)//2-(count1[0]+1)//2)*(K-1))
else:
    print(int(np.sum((count1+1)//2))*K)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
cnt = 0
loop = 0
t = list(s) * k
b = 0
while b < len(t) - 1:
    if len(list(set(t[b:]))) == len(t[b:]):
        break
    if t[b] == t[b + 1]:
        t[b + 1] = '?'
    b += 1
aa = t.count("?")
print(aa)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
cnt = 0
loop = 0
t = list(s) * k
b = 0
while b < len(t) - 1:
    if len(list(set(t[b:]))) == len(t[b:]):
        break
    if t[b] == t[b + 1]:
        t[b + 1] = '?'
    b += 1
aa = t.count("?") * k
print(aa)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
q = s *k
l = [c for c in q]
count = 0
i = 0
while i < len(l)-1:
  if l[i] == l[i+1]:
    i += 2
    count += 1
  else:
    i += 1
    
print(count)

Submission status: Runtime Error

Solution code:
import sys
import os
import itertools
f = open('input.txt', 'r')
sys.stdin = f

S = input()
K = int(input())
#print(type(S), type(K))
last_char = S[:1]
S_from1 = S[1:]
counter = 1
SumOfManip = 0
#文字列が連続する回数のリスト
NumOfSequenceCharList = []

for c in S_from1:
    if(last_char == c):
        counter += 1
    else:
        NumOfSequenceCharList.append(counter)
        counter = 1
    last_char = c

if(counter == len(S)):
    NumOfSequenceCharList.append(counter)
    AllNum = K * NumOfSequenceCharList[0]
    if AllNum % 2 == 0:
        AllManip = AllNum / 2
    else:
        AllManip = (AllNum - 1) / 2
    print(int(AllManip))

else:
    if(counter != 1):
        NumOfSequenceCharList.append(counter)
    #最初と最後の文字が同じなら
    if(S[:1] == S[-1]):
        #偶数ならその数の1/2が操作回数、奇数ならその数-1して1/2にした数が操作回数
        #最初と最後にれんぞくした文字数を足す
        NumFirstPlusLast = NumOfSequenceCharList[0] + NumOfSequenceCharList[-1]
        if NumFirstPlusLast % 2 == 0:
            ManipFirstLast = NumFirstPlusLast / 2
        else:
            ManipFirstLast = (NumFirstPlusLast - 1) / 2
        #最初と最後以外を処理
        for num in NumOfSequenceCharList[1:-1]:
            if num % 2 == 0:
                Manip = num / 2
            else:
                Manip = (num -1) /2
            SumOfManip += Manip
        if NumOfSequenceCharList[0] % 2 == 0:
            Manip0 = NumOfSequenceCharList[0] / 2
        else:
            Manip0 = (NumOfSequenceCharList[0] - 1) / 2
        if NumOfSequenceCharList[-1] % 2 == 0:
            ManipM1 = NumOfSequenceCharList[-1] / 2
        else:
            ManipM1 = (NumOfSequenceCharList[-1] - 1) / 2

        AllManip = Manip0 + (K - 1) * ManipFirstLast + K * SumOfManip +  ManipM1

    else:
        for num in NumOfSequenceCharList:
            if num % 2 == 0:
                Manip = num / 2
            else:
                Manip = (num -1) /2
            SumOfManip += Manip
        AllManip = K * SumOfManip

    #print(AllManip,NumOfSequenceCharList,NumFirstPlusLast,ManipFirstLast,Manip0,SumOfManip,ManipM1,NumOfSequenceCharList,NumOfSequenceCharList[1:-1])
    print(int(AllManip))

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())

buf=''
cnt = 1
total = 0
cnts = []


for s in S:
    #print(buf,s,cnt)
    if s == buf:
        cnt += 1       
    else:
        cnts.append(cnt)
        total += int(cnt/2)
        cnt = 1        
    buf = s
cnts.append(cnt)    
total += int(cnt/2)

if len(set(list(S)))==1:
    print(int(len(S*K)/2))

elif S[0]==S[-1] and cnts[1] % 2 != 0:
    if (cnts[1]+cnts[-1]) % 2 == 0:
        print(total*K+K-1)
    else:
        print(total*K)
else:
    print(total*K)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
cnt = 0
loop = 0
t = [i for i in s]
b = 0
while b < len(t) - 1:
    if len(list(set(t[b:]))) == len(t[b:]):
        break
    if t[b] == t[b + 1]:
        t[b + 1] = '?'
    b += 1
ss = [t[::-1][1], t[::-1][0], t[0], t[1]]
c = 0
while c < len(ss) - 1:
    if ss[c] == ss[c + 1]:
        ss[c + 1] = '!'
    c += 1

aa = t.count("?") * k + ss.count("!")

print(aa)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
cnt = 0
submit = 0
tokusyu = 0
ss = s*2
#print(ss)
len_ss = len(ss)
len_s = len(s)
#print("len_ss",len_ss)
if len(s*k) == 1:
    print("0")
else:
    ato = 1
    while cnt < len_ss-1:
        #print(len_cnt)
        if ss[cnt] == ss[cnt+1]:
            ato = cnt+1
            #print(ss[cnt])
            while ss[cnt] == ss[ato]:
                ato+=1
                #print("ここ",ato)
                if len_ss == ato:
                    if len_ss == ato-cnt:
                        tokusyu = 1
                        cnt = len_ss
                        break

                    submit = submit + (ato-cnt)//2
                    cnt = len_ss
                    break
            submit = submit + (ato-cnt)//2
            #print((ato-cnt)//2)
            cnt = ato
        else:
            cnt+=1
    double_cnt = submit
    submit = 0
    cnt = 0
    ato = 1
        #print("a")
    while cnt < len_s-1:
        #print(len_cnt)
        if s[cnt] == s[cnt+1]:
            ato = cnt+1
            #print(ss[cnt])
            while s[cnt] == s[ato]:
                ato+=1
                #print("ここ",ato)
                if len_s == ato:
                    if len_s == ato-cnt:
                        tokusyu = 1
                        cnt = len_s
                        break

                    submit = submit + (ato-cnt)//2
                    cnt = len_s
                    break
            submit = submit + (ato-cnt)//2
            #print((ato-cnt)//2)
            cnt = ato
        else:
            cnt+=1
    #print(submit)
    #print(len_cnt)
    if tokusyu == 0:
        #print(len_cnt)
        k_double = k//2
        k_single = k%2
        print(k_double*double_cnt+submit*k_single)
    else:
        #print(len_cnt)
        print(len(s*k)//2)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
q = s *k
count = 0
l=[c for c in q]
for i in range(len(l)-1):
  if l[i] == l[i+1]
    count += 1
    
print(count)

Submission status: Runtime Error

Solution code:
import sys

def solveA(S):
    N = len(S)
    count = 0
    for i in range(N-1):
        if S[i] == S[i+1]:
            S[i+1] = "_"
            count += 1
    return count

def solve(S, K):
    S0 = S[:]
    N = len(S)
    if N == 1:
        return (K-(K%2))//2
    if K < 10:
        return solveA(S*K)
    actual=(solveA(S*K))
    S1 = S0*1
    S2 = S0*2
    c1 = solveA(S1)
    c2 = solveA(S2)
    if c1 * 2 == c2:
        return c1 * K
    if S2[0] != S2[-1]:
        return c2 * (K//2) + (K%2) * c1
    else:
        a =  c2 * (K//2) + (K%2)*c1 + (K//2)-(1-K%2)
            #if a != solveA(S0*K):
            #print(c1, c2, S2[0]!=S2[-1])
            #print("c2 * (K//2) +(K%2)*c1=", c2 * (K//2)+(K%2)*c1)
        return a


def main():
    S = list(input().rstrip()) # len(S) <= 100
    K = int(input())    # K <= 10**9

    print(solve(S, K))
def test():
    import random

    for i in range(2000):
        n = random.randint(1,100)
        S = random.choices("aaaaaabcedbdddddklkjlkjdsfaak", k=n)
        S0 = S[:]
        K = random.randint(2, 30)
        SK = S*K
        a, a0 = solve(S, K), solveA(SK)
        if a != a0:
            print("".join(S0))
            print(K)
            print("actual=",a, ",expected=",a0)
            print("BAD\n")
            return
#test()
main()

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
cnt = 0
submit = 0
tokusyu = 0
ss = s*2
#print(ss)
len_ss = len(ss)
len_s = len(s)
#print("len_ss",len_ss)
if len(s*k) == 1:
    print("0")
else:
    ato = 1
    while cnt < len_ss-1:
        #print(len_cnt)
        if ss[cnt] == ss[cnt+1]:
            ato = cnt+1
            #print(ss[cnt])
            while ss[cnt] == ss[ato]:
                ato+=1
                #print("ここ",ato)
                if len_ss == ato:
                    if len_ss == ato-cnt:
                        tokusyu = 1
                        cnt = len_ss
                        break

                    submit = submit + (ato-cnt)//2
                    cnt = len_ss
                    break
            submit = submit + (ato-cnt)//2
            #print((ato-cnt)//2)
            cnt = ato
        else:
            cnt+=1
        double_cnt = submit
        submit = 0
        cnt = 0
        ato = 1
        print("a")
    while cnt < len_s-1:
        #print(len_cnt)
        if s[cnt] == s[cnt+1]:
            ato = cnt+1
            #print(ss[cnt])
            while s[cnt] == s[ato]:
                ato+=1
                #print("ここ",ato)
                if len_s == ato:
                    if len_s == ato-cnt:
                        tokusyu = 1
                        cnt = len_s
                        break

                    submit = submit + (ato-cnt)//2
                    cnt = len_s
                    break
            submit = submit + (ato-cnt)//2
            #print((ato-cnt)//2)
            cnt = ato
        else:
            cnt+=1
    #print(submit)
    #print(len_cnt)
    if tokusyu == 0:
        #print(len_cnt)
        k_double = k//2
        k_single = k%2
        print(k_double*double_cnt+submit*k_single)
    else:
        #print(len_cnt)
        print(len(s*k)//2)