#P5041. [HAOI2009] 求回文串

[HAOI2009] 求回文串

Description

A palindrome is a string that reads the same forwards and backwards. For example, ABCBA is a palindrome, while ABCAB is not. Given any input string, you may repeatedly swap the ii-th character with the (i+1)(i+1)-th character to eventually make the string a palindrome. Find the minimum number of swaps.

Input Format

A string consisting of uppercase letters.

Output Format

If it can be transformed into a palindrome by a finite number of operations, output the minimum number of operations; otherwise output 1-1.

SHLLZSHZS
4

Hint

Sample Explanation:

  1. Swap L\tt L and Z\tt Z to get SHLZLSHZS\tt SHLZLSHZS.
  2. Swap L\tt L and Z\tt Z to get SHZLLSHZS\tt SHZLLSHZS.
  3. Swap L\tt L and S\tt S to get SHZLSLHZS\tt SHZLSLHZS.
  4. Swap H\tt H and Z\tt Z to get SHZLSLZHS\tt SHZLSLZHS.

Constraints:

  • For 40%40\% of the testdata, length 50000\leq50000.
  • For 100%100\% of the testdata, length 106\leq10^6.

Translated by ChatGPT 5