#K13281. String Reduction Challenge

    ID: 23878 Type: Default 1000ms 256MiB

String Reduction Challenge

String Reduction Challenge

You are given a string s consisting only of characters 'a' and 'b'. Your task is to repeatedly reduce the string until only one character remains. The reduction process is defined as follows:

For every two consecutive characters in the string, apply the following rule:

  • If the two characters are the same (aa or bb), they are replaced by that same character.
  • If the two characters are different (ab or ba), they are replaced by the character c.

If the string has an odd length, leave the last character as it is. After one pass of pairwise reduction, assign the newly formed string as the new input string and repeat the process until the length of the string becomes 1.

You may formalize the replacement rule mathematically as follows. For two characters x and y in \( \{a,b\} \), define:

[ R(x,y)= \begin{cases} x, & \text{if } x=y \ c, & \text{if } x \neq y \end{cases} ]

Apply \( R(x,y) \) to every adjacent pair in the string until a single character remains. Output that final character.

inputFormat

The first line contains an integer T representing the number of test cases. Each of the following T lines contains a string consisting only of characters 'a' and 'b'.

Example:

2
ab
aa

outputFormat

For each test case, print the final single character obtained after repeatedly applying the reduction process. Each output should be printed on a new line.

Example:

c
a
## sample
2
ab
aa
c

a

</p>