Palindrome Split

4

13 votes
String Searching, String Algorithms, Algorithms
Problem

You are given a string S consisting of lowercase English letters. Your task is to find the maximum number of palindromic subsequences that can be created from the given string. Each palindromic subsequence must have a length greater than 1, and each element of the string can be part of atmost one palindromic subsequence.

We'll call non-empty string S[p1p2pk]=Sp1Sp2Spk(1p1<p2<<pk|S|) subsequence of string S=S1S2S|s|, where |S| is the length of string S. For example, strings "abcb", "cb" and "abacaba" are subsequences of string "abacaba".

Input format

  • The first line of input contains an integer T denoting the number of test cases.
  • Each test case consists the string S.

Output format

For each test case, print the maximum number of palindromic subsequences you can create in a separate line.

Constraints

1T1002|S|104

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
  • In the first test case you can create one palindromic subsequence. You can create "aa".
  • In the second test case you can create two palindromic subsequence. You can create "dfd", "ff".
Editor Image

?