HihoCoder-1039-character elimination (simulation question ~)

Source: Internet
Author: User

HihoCoder-1039-character elimination (simulation question ~)

#1039: character elimination time limit: 256 ms single point of time limit: Ms memory limit: MB
Description

Little Hi is playing a character elimination game recently. Given a string s that only contains the uppercase letter "ABC", the elimination process is as follows:


1) if s contains substrings with a length greater than 1 consisting of the same letters, these substrings will be eliminated at the same time, and the remaining substrings will be assembled into new strings. For example, "CC", "CCC", and "AA" in "ABCCBCCCAA" are eliminated at the same time, and the remaining "AB" and "B" are combined into a new string "ABB ".

2) The above elimination will be performed repeatedly until the new string does not contain adjacent identical characters. For example, "ABCCBCCCAA" gets "ABB" after elimination, and "A" after elimination"


Each level of the game's Little Hi will face a string s. Before the cancellation, the Little Hi has the opportunity to insert any character ('A ', 'B' or 'C') to obtain the string t. T after a series of elimination, the score of the small Hi is the total number of characters that are eliminated.


Help Xiao Hi calculate how to insert characters to get the highest score.


Input

The first line of the input is an integer T (1 <= T <= 100), representing the number of test data.

Each line following T is A string consisting of 'A' B 'C'. The length cannot exceed 100.


Output

For each line of input strings, output the highest score of the small Hi.


Prompt

First set of data: insert 'C' after 2nd characters of "ABCBCCCAA" to get "ABCCBCCCAA". After elimination, get "", eliminate a total of 9 characters (including the inserted 'C ').

The second group of data: "AAA" insert 'A' to get "AAAA", and then get "", with A total of four characters removed.

The third group of data: whether the characters "AABC", "ABBC", or "ABCC" are inserted, a maximum of two characters can be deleted.


Sample Input
3ABCBCCCAAAAAABC
Sample output
942




It's okay to simulate the past ~~


AC code:

# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
       
# Define LL long longusing namespace std; string s; int del (string p) {// The characters are eliminated once each time they are executed until they cannot be eliminated. int len = p. size (), lent; if (len = 0) return 0; // end condition string t = ""; // Add the remaining values to t after deletion to int l = 0; p + = "$"; // Add a flag for (int I = 1; I <len + 1; I ++) {if (p [I]! = P [I-1]) {if (l = I-1) t + = p [I-1]; l = I ;}} if (lent = t. size () = len) return 0; // return len-lent + del (t); // recursion} int main () {int N; scanf ("% d", & N); while (N --) {cin> s; int ans = 0, t, len = s. size (); for (int I = 0; I <= len; I ++) {string tmp = s; tmp. insert (I, "A"); if (t = del (tmp)> ans) ans = t; tmp = s; tmp. insert (I, "B"); if (t = del (tmp)> ans) ans = t; tmp = s; tmp. insert (I, "C"); if (t = del (tmp)> ans) ans = t ;}cout <ans <endl ;}return 0 ;}
      
     
    
   













Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.