Problem: 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.

Submission attempts (in order):
S=input()
K=int(input())
if len(set(list(S))) == 1:
    print(int(len(S)*K/2))
    exit()
else:  
    s=list(S*2)
    c=0
    for i in range(len(s)-2):
        t=set(s[i:i+2])
        if len(t)==1:
            s[i+1]=""
            c+=1
    if S[0]!=S[-1]:
        print(int(c*K/2))

    else:
        i,h,t=0,1,0
        while S[i]==S[i+1]:
            h+=1
            i+=1
        i=0
        while S[-i]==S[-i-1]:
            t+=1
            i+=1
        print(int(((h+t)*(K-1))/2))

S=input()
K=int(input())
if len(set(S)) == 1:
    print(K/2)
    
s=list(S*2)
c=0
for i in range(len(s)-2):
    t=set(s[i:i+2])
    if len(t)==1:
        s[i+1]=""
        c+=1
if S[0]!=S[-1]:
    print(c*K)
else:
    i,h,t=0,0,0
    while S[i]==S[i+1]:
        h+=1
        i+=1
    i=0
    while S[-i]==S[-i-1]:
        t+=1
        i+=1
    print(c*K-(h/2+t/2-(h+t)/2)**(K-1))

S=input()
K=int(input())
if len(set(list(S))) == 1:
    print(len(S)*K/2)
    exit()
else:  
    s=list(S*2)
    c=0
    for i in range(len(s)-2):
        t=set(s[i:i+2])
        if len(t)==1:
            s[i+1]=""
            c+=1
    if S[0]!=S[-1]:
        print(c*K)

    else:
        i,h,t=0,0,0
        while S[i]==S[i+1]:
            h+=1
            i+=1
        i=0
        while S[-i]==S[-i-1]:
            t+=1
            i+=1
        print(c*K-(h/2+t/2-(h+t)/2)**(K-1))

S=input()
K=int(input())
if len(set(list(S))) == 1:
    print(int(len(S)*K/2))
    exit()
else:  
    s=list(S*2)
    c=0
    for i in range(len(s)-2):
        t=set(s[i:i+2])
        if len(t)==1:
            s[i+1]=""
            c+=1
    if S[0]!=S[-1]:
        print(int(c*K/2))

    else:
        i,h,t=0,0,0
        while S[0]==S[i]:i+=1;h+=1
        i=1
        while S[-1]==S[-i]:t+=1;i+=1
        
        print(int(c*K/2-(h/2+t/2-(h+t)/2)*(K-1)))
            

S=list(input())
K=int(input())
l=[1]
for i in range(len(S)-1):
    if S[i]==S[i+1]:l[-1]+=1;
    else:l.append(1)
if len(l)==1:ans = len(S)*K/2;
else:
    l2=[i/2 for i in l]
    if S[0]==S[-1]:
        ht=l(l[0]+l[-1])//2
        ans=ls2.pop(0)
        ans+=l2.pop(-1)
        ans+=sum(l2)*K
        ans+=ht*(K-1)
    else:
        ans=sum(ls)*K
print(ans)
    


S=input()
K=int(input())
if len(set(list(S))) == 1:
    print(int(len(S)*K/2))
    exit()
else:  
    s=list(S*2)
    c=0
    for i in range(len(s)-2):
        t=set(s[i:i+2])
        if len(t)==1:
            s[i+1]=""
            c+=1
    if S[0]!=S[-1]:
        print(c*K)

    else:
        i,h,t=0,0,0
        while S[i]==S[i+1]:
            h+=1
            i+=1
        i=0
        while S[-i]==S[-i-1]:
            t+=1
            i+=1
        print(c*K-(h/2+t/2-(h+t)/2)**(K-1))

S=input()
K=int(input())
if len(set(list(S))) == 1:
    print(int(len(S)*K/2))
    exit()
else:  
    s=list(S*2)
    c=0
    for i in range(len(s)-2):
        t=set(s[i:i+2])
        if len(t)==1:
            s[i+1]=""
            c+=1
    if S[0]!=S[-1]:
        print(int(c*K))

    else:
        i,h,t=0,0,0
        while S[i]==S[i+1]:
            h+=1
            i+=1
        i=0
        while S[-i]==S[-i-1]:
            t+=1
            i+=1
        print(int((h+t)*(K-1)))

S=input()
K=int(input())
if len(set(list(S))) == 1:
    print(K/2)
    exit()
else:  
    s=list(S*2)
    c=0
    for i in range(len(s)-2):
        t=set(s[i:i+2])
        if len(t)==1:
            s[i+1]=""
            c+=1
    if S[0]!=S[-1]:
        print(c*K)

    else:
        i,h,t=0,0,0
        while S[i]==S[i+1]:
            h+=1
            i+=1
        i=0
        while S[-i]==S[-i-1]:
            t+=1
            i+=1
        print(c*K-(h/2+t/2-(h+t)/2)**(K-1))

S=input()
K=int(input())
s=list(S*2)
c=0
if len(set(list(S))) == 1:
    print(int(len(S)*K/2))
else:
    for i in range(len(s)-2):
        t=set(s[i:i+2])
        if len(t)==1:
            s[i+1]=""
            c+=1
    if s[0]==s[-1]:c+=1
    print(int(c*(K-1)/2))


S=list(input())
K=int(input())
l=[1]
for i in range(len(S)-1):
    if S[i]==S[i+1]:l[-1]+=1;
    else:l.append(1)
if len(l)==1:ans = len(S)*K/2;
else:
    l2=[i/2 for i in l]
    if S[0]==S[-1]:
        ht=(l[0]+l[-1])//2
        ans=l2.pop(0)
        ans+=l2.pop(-1)
        ans+=sum(l2)*K
        ans+=ht*(K-1)
    else:
        ans=sum(l2)*K
print(ans)
    


S=list(input())
K=int(input())
l=[1]
for i in range(len(S)-1):
    if S[i]==S[i+1]:l[-1]+=1;
    else:l.append(1)
if len(l)==1:ans = len(S)*K//2;
else:
    l2=[i//2 for i in l]
    if S[0]==S[-1]:
        ht=(l[0]+l[-1])//2
        ans=l2.pop(0)
        ans+=l2.pop(-1)
        ans+=sum(l2)*K
        ans+=ht*(K-1)
    else:
        ans=sum(l2)*K
print(int(ans))
    


Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted