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 ;}