Problem:


Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and  abcba are palindromes, while ab, abab and abcda are not.

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx



Sample input:
xabxa


Sample output:
2

Submission status: Runtime Error

Solution code:
import collections

s = input()
c = collections.Counter(s)

flag = True
odd_count = 0
for char, times in c.items():
    if char == "x":
        continue

    if times % 2 == 0:
        continue

    odd_count += 1
    odd_char = char
    if odd_count >= 2:
        flag = False
        break

if flag:
    x_count = 0

    if odd_count == 0:
        s_split = s.split("x")
    else:
        s_split = s.split(odd_char)

    s_form = s_split[0]
    s_latt = s_split[1]
    
    s_form_rev = collections.deque(s_form[::-1])
    s_latt = collections.deque(s_latt)

    while len(s_form_rev) > 0 or len(s_latt) > 0:
        if len(s_form_rev) == 0:
            for char in s_latt:
                if char == "x":
                    x_count += 1
                else:
                    flag = False
                    break
            break
        elif len(s_latt) == 0:
            for char in s_form_rev:
                if char == "x":
                    x_count += 1
                else:
                    flag = False
                    break
            break

        if s_form_rev[0] == s_latt[0]:
            s_form_rev.popleft()
            s_latt.popleft()
            continue

        if s_form_rev[0] == "x":
            s_form_rev.popleft()
            x_count += 1
            continue

        if s_latt[0] == "x":
            s_latt.popleft()
            x_count += 1
            continue

        flag = False
        break

if flag:
    print(x_count)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# a〜z -- START --
l=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
# a〜z --- END ---

def main():
  s=S()

  _s=s.replace('x','')

  # 可能か
  if _s!=''.join([x for x in _s[::-1]]):
    return -1

  center=_s[len(_s)//2]
  to_center_count=0

  for x in _s[len(_s)//2:]:
    if x==center:
      to_center_count+=1

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

    if count==to_center_count:
      ptr1=i
      break

  ptr2=ptr1
  if len(_s)%2==0:
    while True:
      ptr1-=1
      if s[ptr1]!='x':
        break

  ans=0
  while True:
    if ptr1==-1:
      return len(s)-ptr2+ans
    if ptr2==len(s):
      
      return ptr1+1+ans

    if s[ptr1]=='x':
      ptr1-=1
      if s[ptr2]=='x':
        ptr2+=1
      else:
        ans+=1
    elif s[ptr2]=='x':
      ptr2+=1
      if s[ptr1]=='x':
        ptr1-=1
      else:
        ans+=1
    else:
      ptr1-=1
      ptr2+=1

# main()
print(main())

Submission status: Runtime Error

Solution code:
# x無視したやつが回文なってればいいんじゃね？
# 真ん中にしたい(x以外の)文字が決まれば、もとのsにインデックスを適応して、左右のxの数の差をとればいい
# と思ったけど差をとっただけだと駄目だな xaaxxxbxxcxxxxbxaxxaxx はこの考えだと3だけど実際は7だし
# もうしかたねぇから無理やりsplitした（アホか？）

import re

s = input()
if len(set(s)) == 1:
    # 1文字でのみ構成されている場合
    print(0)
    exit()
t = ""
for sv in s:
    if sv != "x":
        t += sv

if t != t[::-1]:
    # 回文にならない場合
    print(-1)
    exit()

mid_char = t[len(t) // 2]
mid_count = t.count(mid_char) // 2
mid_idx = -1
target_idx = 0
for i in range(len(s)):
    if s[i] == mid_char:
        mid_idx += 1
    if mid_idx == mid_count:
        target_idx = i
        break
ss, tt = s[:target_idx], s[target_idx + 1 :]
tt = tt[::-1]

ss = re.split("[abcdefghijklmnopqrstuvwyz]", ss)
tt = re.split("[abcdefghijklmnopqrstuvwyz]", tt)
ans = 0
for i in range(len(ss)):
    ans += abs(len(ss[i]) - len(tt[i]))

print(ans)

Submission status: Runtime Error

Solution code:
# x無視したやつが回文なってればいいんじゃね？
# 真ん中にしたい(x以外の)文字が決まれば、もとのsにインデックスを適応して、左右のxの数の差をとればいい
s = input()

t = ""
for sv in s:
    if sv != "x":
        t += sv

if t != t[::-1]:
    print(-1)
    exit()

mid = t[len(t) // 2]
mid_c = t.count(mid) // 2
leftx, rightx = 0, 0
count = 0
for sv in s:
    if sv == "x" and count < mid_c:
        leftx += 1
    elif sv == "x" and mid_c <= count:
        rightx += 1
    elif sv == mid and count < mid_c:
        count += 1

print(abs(rightx - leftx))

Submission status: Runtime Error

Solution code:
s = input()
l = 0
r = len(s)-1
ans = 0
while l < r:
    while s[l] == 'x'or s[r] == 'x':
        if s[l] != s[r]:
            ans += 1
        if s[l] == 'x':
            l += 1
        if s[r] == 'x':
            r -= 1
    if s[l] != s[r]:
        break
    l += 1
    r -= 1
else:
    print(ans)
    exit()
print(-1)

Submission status: Runtime Error

Solution code:
s = input()
l = 0
r = len(s)-1
c = 0
while l < r:
  c_l = 0
  c_r = 0
  while s[l] == "x":
    c_l += 1
    l += 1
  while s[r] == "x":
    c_r += 1
    r -= 1
  if s[l] != s[r]:
    print(-1)
    exit()
  c += abs(c_l-c_r)
  l += 1
  r -= 1
print(c)

Submission status: Runtime Error

Solution code:
s = input()

i = 0
j = len(s) - 1
ans = 0
while i != j:
    if s[i] != s[j]:
        if s[i] != "x" and s[j] != "x":
            print(-1)
            exit()
        elif s[i] != "x":
            j -= 1
        else:
            i += 1
        ans += 1
    else:
        i += 1
        j -= 1

print(ans)

Submission status: Runtime Error

Solution code:
import math
s = input().strip()
A = []
B = [0 for _ in range(len(s)+1)]
y = ""
for i in range(len(s)):
    if s[i]!="x":
        y += s[i]
        A.append(i)
        B[i+1] = B[i]
    else:
        B[i+1] = B[i]+1
flag=0
cnt = 0
for i in range(len(y)//2):
    if y[i]!=y[-(i+1)]:
        flag=1
        cnt = -1
        break
if flag==0:
    if B[A[0]]!=B[-1]-B[A[-1]]:
        cnt += abs(B[A[0]]-(B[-1]-B[A[-1]]))
    for i in range(1,math.ceil(len(A)/2)):
        if B[A[i]]-B[A[i-1]] != B[A[-i]]-B[A[-(i+1)]]:
            cnt += abs(B[A[i]]-B[A[i-1]]-(B[A[-i]]-B[A[-(i+1)]]))
print(cnt)

Submission status: Runtime Error

Solution code:
s = input()

l = 0
r = len(s) - 1
answer = 0

while l != r:
    if s[l] == s[r]:
        l += 1
        r -= 1
    elif s[l] == 'x':
        l += 1
        answer += 1
    elif s[r] == 'x':
        r -= 1
        answer += 1
    else:
        print(-1)
        exit()

print(answer)

Submission status: Runtime Error

Solution code:
from collections import deque

s = deque(input())
answer = 0

if len(s) == 1:
    print(0)
    exit()

l = ''
r = ''

while l == '' or r == '':
    if s:
        if l == '':
            l = s.popleft()
        if r == '':
            r = s.pop()

    if l == r:
        l = ''
        r = ''
    elif l == 'x':
        answer += 1
        l = ''
    elif r == 'x':
        answer += 1
        r = ''
    elif r == '' or l == '':
        break
    else:
        print(-1)
        exit()

print(answer)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
S = input()

#
can_solve = True
m = [s for s in S if s != 'x']
len_m = len(m)
for i in range(len_m):
    if m[i] != m[len_m - i - 1]:
        can_solve = False
        break

#
if can_solve:
    nums = [0 for _ in range(len_m+1)]
    i, n = 0, 0
    for s in S:
        if s == 'x':
            n += 1
        else:
            nums[i] = n
            i += 1
            n = 0
    nums[i] = n

    len_nums = len_m+1
    ans = 0
    for i in range(len_nums // 2):
        ans += max(nums[i], nums[len_nums - i - 1])
    if len_nums % 2 == 1:
        ans += nums[len_nums // 2 + 1] % 2
    # print(nums)
    print(ans)


else:
    print(-1)

Submission status: Runtime Error

Solution code:
# 2019-11-21 01:26:03(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r, insort_left as in_l
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 

def is_palindrome_in(s):
    s = s.replace('x', '')
    if list(s) == list(reversed(s)):
        return True
    else:
        return False

def main():
    s = sys.stdin.readline().rstrip()
    
    if not is_palindrome_in(s):
        print(-1)
    else:
        s = collections.deque(s)
        count = 0
        i, j = 0, 0
        while i < len(s) - 1 - j:
            if s[i] == 'x':
                if s[-1-j] != 'x':
                    s.insert(len(s)-j, 'x')
                    count += 1
            elif s[-1-j] == 'x':
                s.insert(i, 'x')
                count += 1
            
            i += 1
            j += 1

        print(count)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# 2019-11-21 01:26:03(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r, insort_left as in_l
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 

def is_palindrome_in(s):
    s = s.replace('x', '')
    if list(s) == list(reversed(s)):
        return True
    else:
        return False

def main():
    s = sys.stdin.readline().rstrip()
    
    if not is_palindrome_in(s):
        print(-1)
        sys.exit()
        
    s = collections.deque(s)
    count = 0
    i, j = 0, 0
    while i < len(s) - 1 - j:
        if s[i] == 'x':
            if s[-1-j] != 'x':
                s.insert(len(s)-j, 'x')
                count += 1
        elif s[-1-j] == 'x':
            s.insert(i, 'x')
            count += 1
        
        i += 1
        j += 1

    print(count)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# 2019-11-21 01:26:03(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r, insort_left as in_l
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 

def is_palindrome_in(s):
    s = s.replace('x', '')
    if s == ''.join(reversed(s)):
        return True
    else:
        return False

def main():
    s = sys.stdin.readline().rstrip()
    
    if not is_palindrome_in(s):
        print(-1)
        exit()

    s = collections.deque(s)
    print(s)
    count = 0
    i, j = 0, 0
    while i < len(s) - 1 - j:
        if s[i] == 'x':
            if s[-1-j] == 'x':
                pass
            else:
                s.insert(len(s)-j, 'x')
                count += 1
        else:
            if s[-1-j] == 'x':
                s.insert(i, 'x')
                count += 1
        
        i += 1
        j += 1

    print(count)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# 2019-11-21 01:26:03(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r, insort_left as in_l
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 

def is_palindrome_in(s):
    s = s.replace('x', '')
    if s == ''.join(reversed(s)):
        return True
    else:
        return False

def main():
    s = sys.stdin.readline().rstrip()
    
    if not is_palindrome_in(s):
        print(-1)
        sys.exit()

    s = collections.deque(s)
    count = 0
    i, j = 0, 0
    while i < len(s) - 1 - j:
        if s[i] == 'x':
            if s[-1-j] != 'x':
                s.insert(len(s)-j, 'x')
                count += 1
        elif s[-1-j] == 'x':
            s.insert(i, 'x')
            count += 1
        
        i += 1
        j += 1

    print(count)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
S = input()

answer = 0

def F(s):
    global answer
    if len(s) <= 1:
        return
    if s[0] == 'x':
        if s[-1] == 'x':
            F(s[1:-1])
        else:
            answer += 1
            F(s[1:])
    else:
        if s[-1] == 'x':
            answer += 1
            F(s[:-1])
        elif s[0] == s[-1]:
            F(s[1:-1])
        else:
            print(-1)
            exit()

F(S)
print(answer)

Submission status: Runtime Error

Solution code:
S = list(input())
i, j = 0, len(S) - 1
ans = 0

while i != j:
    if S[i] == S[j]:
        i += 1
        j -= 1

    elif S[i] == 'x':
        i += 1
        ans += 1

    elif S[j] == 'x':
        j -= 1
        ans += 1

    else:
        print('-1')
        break

else:
    print(ans)

Submission status: Runtime Error

Solution code:
S = input()


l = 0
r = len(S)-1
cnt = 0
while l != r:
    if S[l] == S[r]:
        l += 1
        r -= 1
    else:
        if S[l] == "x":
            #末尾にxを追加し削除
            l += 1
            cnt += 1
        elif S[r] == "x":
            r -= 1
            cnt += 1
        else:
            print(-1)
            exit()


print(cnt)

Submission status: Runtime Error

Solution code:
s = input()
lens = len(s)
l = 0
r = lens - 1
cnt = 0
while l != r:
  if s[l] == s[r]:
    l += 1
    r -= 1
  elif s[l] == 'x':
    cnt += 1
    l += 1
  elif s[r] == 'x':
    cnt += 1
    r -= 1
  else:
    print(-1)
    exit()
print(cnt)

Submission status: Runtime Error

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heapify,heappop,heappush
import sys,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

s = input()
n = len(s)
snx = []

for i in range(n):
    if s[i] != 'x':
        snx.append(s[i])
ln = len(snx)
for i in range(ln//2):
    if snx[i] != snx[ln-i-1]:
        print(-1)
        quit()

j = 0
if ln%2:
    for l in range(n):
        if s[l] == snx[j]:
            j += 1
            if j >= ln//2+1:  
                break
    print(n-l-l-1)
else:
    for l in range(n):
        if s[l] == snx[j]:
            j += 1
            if j >= ln//2:  
                break 
    j = ln-1
    for r in range(n)[::-1]:
        if s[r] == snx[j]:
            j -= 1
            if j <= ln//2-1:
                break

    print(abs(n-r-l-1))

Submission status: Runtime Error

Solution code:
S = input()
org = str(S)
count = S.count("x")
S = S.replace("x", "")
left = S[:len(S)//2+1]
right = S[len(S)//2:][::-1]
if left == right:
  i = 0
  count_left = 0
  while (i - count_left) < (len(S)//2+1):
    if S[i] == "x":
      count_left += 1
    i += 1
  print(abs(count - count_left))
else:
  print(-1)

Submission status: Runtime Error

Solution code:
S = input()
org = str(S)
count = S.count("x")
S = S.replace("x", "")
left = S[:len(S)//2+1]
right = S[:len(S)//2+1][::-1]
if left == right:
  i = 0
  count_left = 0
  while (i - count_left) < (len(S)//2+1):
    if S[i] == "x":
      count_left += 1
    i += 1
  print(abs(count - count_left))
else:
  print(0)

Submission status: Runtime Error

Solution code:
#inserting X
s=input()
length=len(s)
string=""
for i in range(length):
    if s[i]!="x":
        string=string+s[i]
    else:
        continue

flag=True
length2=len(string)
if length2>=2:
    for i in range(length//2):
        if string[i]!=string[-(i+1)]:
            flag=False
            break
if flag:
    s="x"+s+"X"
    length=len(s)
    checklist=[]
    count=0
    for i in range(length):
        if s[i]=="x":
            count+=1
        else:
            checklist.append(count)
            count=0
    checklist[0]-=1
    ans=0
    for i in range(len(checklist)//2):
        ans+=max(checklist[i],checklist[-(i+1)])
    print(ans)
    
    
else:
    print("-1")

Submission status: Runtime Error

Solution code:
S = list(input())
invS = []
for s in reversed(S):
    invS.append(s)
cnt = 0
loop = 0
i,j = 0,0
while i+j != len(S)-1:
    if S[i] == invS[j]:
        i += 1
        j += 1
    elif S[i] == "x":
        cnt += 1
        i += 1
    elif invS[j] == "x":
        cnt += 1
        j += 1
    else:
        cnt = -1
        break
print(cnt)

Submission status: Runtime Error

Solution code:
S = list(input())
invS = []
for s in reversed(S):
    invS.append(s)
cnt = 0
loop = 0
i,j = 0,0
while i <= len(S)//2+1 and j <= len(S)//2+1:
    if S[i] == invS[j]:
        i += 1
        j += 1
    elif S[i] == "x":
        cnt += 1
        i += 1
    elif invS[j] == "x":
        cnt += 1
        j += 1
    loop += 1
    if loop > len(S):
        cnt = -1
        break
print(cnt)

Submission status: Runtime Error

Solution code:
import sys
s = input()
# まず達成可能かどうかを判定する
s_without_x = [si for si in s if si != 'x']
if len(s_without_x) % 2 == 0:
    if s_without_x[:len(s_without_x)//2] != s_without_x[len(s_without_x)-1:len(s_without_x)//2-1:-1]:
        print(-1)
        sys.exit(0)
else:
    if s_without_x[:len(s_without_x)//2] != s_without_x[len(s_without_x)-1:len(s_without_x)//2:-1]:
        print(-1)
        sys.exit(0)

# 達成可能な場合
l = 0
r = len(s) - 1
cnt = 0
while l != r:
    if s[l] != s[r]:
        cnt += 1
        if s[l] == 'x':
            l += 1
        else:
            r -= 1
    else:
        l += 1
        r -= 1
print(cnt)

Submission status: Runtime Error

Solution code:
s = input()

# x を除いたときに回文になっていなければ達成不可能
if s.replace('x', '') != s.replace('x', '')[::-1]:
    print(-1)
    exit()

ans = 0

l = 0
r = len(s)-1

while l != r:
    if s[l] == s[r]:
        l += 1
        r -= 1
    elif s[l] == 'x':
        ans += 1
        l += 1
    elif s[r] == 'x':
        ans += 1
        r -= 1
    else:
        print('error')
        exit()
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
left = 0
right = len(s) - 1
x_left = 0
x_right = 0
if len(s) >= 2:
    while (left <= right):
        if s[left] == s[right]:
            left += 1
            right -= 1
        elif s[left] == "x":
            x_left += 1
            left += 1
        elif s[right] == "x":
            x_right += 1
            right -= 1
        if (right - left == 1) & (s[left] != s[right]):
            if (s[left] != "x") & (s[right] != "x"):
                print(-1)
                exit()
            else:
                print(x_left + x_right + 1)
                exit()
    print(x_left + x_right)
else:
    print(0)

Submission status: Runtime Error

Solution code:
s = input()
left = 0
right = len(s) - 1
x_left = 0
x_right = 0
if len(s) >= 2:
    while (left <= right):
        if s[left] == s[right]:
            left += 1
            right -= 1
        elif s[left] == "x":
            x_left += 1
            left += 1
        elif s[right] == "x":
            x_right += 1
            right -= 1
        if (right - left == 1) & (s[left] != s[right]):
            if (s[left] != "x") & (s[right] != "x"):
                print(-1)
                exit()
    print(x_left + x_right)
else:
    print(0)

Submission status: Runtime Error

Solution code:
s = input()
left = 0
right = len(s) - 1
x_left = 0
x_right = 0
if len(s) > 2:
    while (left <= right):
        if s[left] == s[right]:
            left += 1
            right -= 1
        elif s[left] == "x":
            x_left += 1
            left += 1
        elif s[right] == "x":
            x_right += 1
            right -= 1
        if (right - left == 1) & (s[left] != s[right]):
            if (s[left] != "x") & (s[right] != "x"):
                print(-1)
                exit()
    print(x_left + x_right)
else:
    print(0)

Submission status: Runtime Error

Solution code:
s = input()
num_x = s.count('x')
s_remove_x = s.replace('x', '', num_x)

if len(s) % 2 == 1:
  if s[(len(s) + 1) // 2] == 'x':
    ans = num_x - 1
else:
  ans = num_x

# s から x を除いたものが回文になっているか判定
half_len = len(s_remove_x) // 2
for i in range(half_len):
  if s_remove_x[i] != s_remove_x[len(s_remove_x)-1-i]:
    ans = -1

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
s = input()
temp = s
temp = temp.replace("x","")
is_rev = True

mirro = []
for i in range(len(temp)//2):
    if temp[i] == temp[-i-1]:
        mirro.append(temp[i])
        continue
    else:
        is_rev = False
if len(temp) %2 == 1:
    mirro.append(temp[len(temp)//2])

if is_rev:
    ind = 0
    i = 0
    lx = np.array([0 for i in range(len(temp)//2+len(temp)%2)])
    rx = np.array([0 for i in range(len(temp)//2+len(temp)%2)])
    temp = 0
    while (True):
        if s[i] == mirro[ind]:
            lx[ind] = temp
            temp = 0
            ind += 1
            i += 1
        else:
            temp += 1
            i += 1

        if ind > len(mirro)-1:
            break
    ind = 0
    i = -1
    temp = 0
    while (True):
        if s[i] == mirro[ind]:
            rx[ind] = temp
            temp = 0
            ind += 1
            i -= 1
        else:
            temp += 1
            i -= 1

        if ind > len(mirro)-1:
            break
    # print(lx)
    # print(rx)
    print(np.sum(np.abs(lx-rx)))
else:
    print(-1)

Submission status: Runtime Error

Solution code:
# code-festival-2017-qualcC - Inserting 'x'
def main():
    S = input().rstrip()
    ans, l, r = 0, 0, len(S) - 1
    while l != r:
        if S[l] == S[r]:
            l += 1
            r -= 1
        else:
            ans += 1
            if S[l] == "x":
                l += 1
            elif S[r] == "x":
                r -= 1
            else:
                print(-1)
                return
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


# 回文判定
def judge(s):
    news = ""
    for si in s:
        if si != 'x':
            news += si

    if news == news[::-1]:
        return True
    else:
        return False

# 操作の最小値
def min_insertion(s):
    news = ""
    for si in s:
        if si != 'x':
            news += si

    n = len(news)
    fir = [0] * ((n+1)//2)
    lat = [0] * ((n + 1) // 2)

    # 前半
    cursor = 0
    for si in s:
        if si == 'x':
            fir[cursor] += 1
        else:
            cursor += 1

        if cursor == (n+1)//2:
            break

    # 後半
    cursor = 0
    for si in s[::-1]:
        if si == 'x':
            lat[cursor] += 1
        else:
            cursor += 1

        if cursor == (n + 1) // 2:
            break

    ans = 0
    for firi, lati in zip(fir, lat):
        ans += abs(firi - lati)

    return ans


# 入力
s = ns()

# x以外の回文判定
is_kaibun = judge(s)

# 回文でないならおわり
# 回文ならば最小値出す
if is_kaibun:
    print(min_insertion(s))
else:
    print(-1)

Submission status: Runtime Error

Solution code:

S = input()

T = []

x_list = []

temp = 0
for i in range(len(S)):
    if S[i]!="x":
        T.append(S[i])
        x_list.append(temp)
        temp=0
    else:
        temp+=1
x_list.append(temp)

U = T[::-1]
flag=False
for i in range(len(T)):
    if T[i]!=U[i]:
        flag=True
        break

if flag:
    print(-1)
else:
    count=0
    for i in range(len(x_list)//2):
        count+=abs(x_list[i]-x_list[len(x_list)-i])

    print(count)

Submission status: Runtime Error

Solution code:
from collections import deque

s = input()

ns = ""

for s_i in s:
    if s_i != "x":
        ns += s_i

if ns != ns[::-1]:
    print(-1)
    exit()

if len(ns) % 2 == 1:
    cnt = -1
    for i,s_i in enumerate(s):
        if s_i != "x":
            cnt += 1
        if cnt == len(ns)//2:
            note = i
            break
    left = deque(s[:note][::-1] + "@")
    right = deque(s[note+1:] + "@")
else:
    cnt = -1
    f = 1
    for i,s_i in enumerate(s):
        if s_i != "x":
            cnt += 1
        if cnt == len(ns)//2 - 1 and f:
            l = i
            f = 0
        if cnt == len(ns)//2:
            r = i
            break
    left = deque(s[:l][::-1] + "@")
    right = deque(s[r+1:] + "@")

ans = 0

while left or right:
    if left[0] == right[0]:
        left.popleft()
        right.popleft()
    elif left[0] == "x":
        left.popleft()
        ans += 1
    elif right[0] == "x":
        right.popleft()
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

s = input()

ns = ""

for s_i in s:
    if s_i != "x":
        ns += s_i

if ns != ns[::-1]:
    print(-1)
    exit()

if len(ns) % 2 == 1:
    cnt = -1
    for i,s_i in enumerate(s):
        if s_i != "x":
            cnt += 1
        if cnt == len(ns)//2:
            note = i
            break
    left = deque(s[:note][::-1] + "@")
    right = deque(s[note+1:] + "@")
else:
    cnt = -1
    f = 1
    for i,s_i in enumerate(s):
        if s_i != "x":
            cnt += 1
        if cnt == len(ns)//2 - 1 and f:
            l = i
            f = 0
        if cnt == len(ns)//2:
            r = i
            break
    left = deque(s[:l][::-1] + "@")
    right = deque(s[r+1:] + "@")

ans = 0

while left or right:
    print(left,right)
    if left[0] == right[0]:
        left.popleft()
        right.popleft()
    elif left[0] == "x":
        left.popleft()
        ans += 1
    elif right[0] == "x":
        right.popleft()
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
s=input()
i=0
j=len(s)-1
c=0
while i>=j:
    if s[i]!=s[j]:
        c+=1
        if s[i]=="x":
            i+=1
        elif s[j]=="x":
            j-=1
        else:
            print(-1)
            break
    else:
        i+=1
        j-=1
else:
    print(c)

Submission status: Runtime Error

Solution code:
s=input()
i=0
j=len(s)-1
c=0
while i!=j:
    if s[i]!=s[j]:
        c+=1
        if s[i]=="x":
            i+=1
        elif s[j]=="x":
            j-=1
        else:
            print(-1)
            break
    else:
        i+=1
        j-=1
else:
    print(c)

Submission status: Runtime Error

Solution code:
from collections import deque
s = deque(input())
print(s)
le = len(s)
cnt = 0
while le:
  if s[0] == s[-1]:
    s.pop()
    s.popleft()
    le -= 2
  elif s[0] == "x":
    s.popleft()
    cnt += 1
    le -= 1
  elif s[-1] == "x":
    s.pop()
    cnt += 1
    le -= 1
  else:
    print(-1)
    break
  print(s)
else:
  print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque
s = deque(input())
le = len(s)-1
cnt = 0
while le:
  if s[0] == s[-1]:
    s.pop()
    s.popleft()
    le -= 2
  elif s[0] == "x":
    s.popleft()
    cnt += 1
    le -= 1
  elif s[-1] == "x":
    s.pop()
    cnt += 1
    le -= 1
  else:
    print(-1)
    break
else:
  print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque
s = deque(input())
cnt = 0
while s:
  if s[0] == s[-1] == "x":
    s.pop()
    s.popleft()
  elif s[0] == "x":
    s.popleft()
    cnt += 1
  elif s[-1] == "x":
    s.pop()
    cnt += 1
  else:
    print(-1)
    break
else:
  print(cnt)

Submission status: Runtime Error

Solution code:

import sys
from collections import deque
import copy
import math

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [S] = [(input_raw[0])]
    head = 0
    tail = len(S) - 1
    count = 0
    #for i in range(len(S)/2 + len(S) % 2):
    while 1:
        if head == tail:
            break
        if S[head] != S[tail]:
            if S[head] == "x":
                count += 1
                head += 1
            elif S[tail] == "x":
                count += 1
                tail -= 1
            else:
                print -1
                return
        else:
            head += 1
            tail -= 1
    print count


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
s=input()
baks=s[:]
s="".join(list(filter(lambda x:x!="x",s)))
revs="".join(list(reversed(s)))
i=0 
j=len(s-1)
ans=0
while i<=j:
  if s[i]==s[j]:
    i+=1
    j-=1
  elif s[i]=="x":
    ans+=1
    i+=1
  elif s[j]=="x":
    ans+=1
    j-=1
  else:  break
if i <=j :
  print(-1)
  exit(0) 
else: print(ans)

Submission status: Runtime Error

Solution code:
s = list(input())
without_x = []
notx_index = []
for i in range(len(s)):
	if s[i] != "x":
		without_x.append(s[i])
		notx_index.append(i)
if without_x != without_x[::-1]:
	print(-1)
else:
	n = len(notx_index)
	#center_ind = (n-1) // 2
	x_num = [notx_index[0]]
	for i in range(n-1):
		x_num.append(notx_index[i+1] - notx_index[i] - 1)
	x_num.append(len(s)-1 - notx_index[n-1])
	#print(x_num)
	ans = 0
	for i in range(n // 2 + 1):
		ans += abs(x_num[i] - x_num[n-i])

	print(ans)

Submission status: Runtime Error

Solution code:
S = list(input())
X, A = [], []
NX = ""

for i in range(len(S)):
    if S[i] == "x": X.append(i)
    else: 
        NX += S[i]
        A.append(i)
    
for i in range(len(NX)//2):
    if NX[i] != NX[-1-i]:
        print(-1)
        break
else:
    if NX == "": print(0)
    else:
        l, r = 0, len(S)-1
        midl = A[len(NX)//2]
        midr = (A[len(NX)//2] if len(NX)%2==1 else A[len(NX)//2 + 1])
        Add = 0
        while l < midl or r > midr:
            if S[l] == S[r] == "x":
                l += 1
                r -= 1
            elif S[l] == "x":
                l += 1
                Add += 1
            elif S[r] == "x":
                r -= 1
                Add += 1
            else:
                l += 1
                r -= 1
        print(Add)