https://atcoder.jp/contests/code-festival-2017-qualc/tasks/code_festival_2017_qualc_c

目標が達成可能かどうかは、s から ‘x’ を取り除いてできる文字列が回文であるかどうかで判定できる。 以後、目標が達成可能な場合を考える。

s の ‘x’ でない文字列のうち左から i 番目の文字を cs[i]cs[i] とする。(i = 1,…,m とする)

ds[i]ds[i]cs[i]cs[i]cs[i+1]cs[i+1] の間にある ‘x’ の個数とする。 ds[0]ds[0]cs[1]cs[1] の前にある ‘x’ の数、ds[m]ds[m]cs[m]cs[m] の後にある ‘x’ の数と拡張しておく。

s = “xabxa” の時は、cs = [‘a’, ‘b’, ‘a’], ds = [1,0,1,0]である。

‘x’ を挿入する操作は、ds の一つの数字を +1 することに相当する。ds が左右対称となった時文字列が回文になるから、 答えは l = length(ds) として i=1l/2ds[i]ds[li+1]\sum_{i=1}^{\lfloor l/2 \rfloor}|ds[i] - ds[l-i+1]| で求められる。

https://atcoder.jp/contests/code-festival-2017-qualc/submissions/32267860