Hihocoder #1039: Character elimination (String processing Class) A long time ago to do the topic, the specific algorithm code read it

Source: Internet
Author: User

#1039: Character elimination time limit:1000msSingle Point time limit:1000msMemory Limit:256MBDescribe

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

1) If s contains a substring of the same letter length of more than 1, then the substrings are eliminated at the same time, and the remaining substrings are spelled into a new string. For example "ABCCBCCCAA" in "CC", "CCC" and "AA" will be eliminated at the same time, the remaining "AB" and "B" to spell the new string "ABB".

2) The above elimination will be repeated round and round until the new string does not contain adjacent identical characters. For example "ABCCBCCCAA" after a round of elimination to get "ABB", and then after a round of elimination to get "A"

Every little hi in the game will face a string s. Before the elimination begins, little Hi has the opportunity to insert any character (' A ', ' B ' or ' C ') anywhere in S (before the first character, after the last character, and next to two characters) to get the string T. After a series of elimination, the score of small hi is the total number of characters eliminated.

Please help small hi calculate how you want to insert characters to get the highest score.

Input

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

A string s of ' a ' B ' C ' that is not more than 100 in length after each line of T line.

Output

For each line of input string, output small hi highest can get the score.

Tips

The first set of data: "ABCBCCCAA" in the 2nd word specifier insert ' C ' to get "ABCCBCCCAA", eliminate the "A", a total elimination of 9 characters (including the inserted ' C ').

The second set of data: "AAA" Insert ' A ' to get "AAAA", after elimination to get "", total elimination of 4 characters.

The third set of data: Whether you get "AABC" after inserting a character, "ABBC" or "ABCC" will eliminate up to 2 characters.

Sample input
3ABCBCCCAAAAAABC
Sample output
942

#include <stdio.h> #include <string.h>int f[100002];int c[600];int w[600];int max (int a, int b) {    return a >B?A:B;} int main () {    int n, m;    int I, j, K;    while (scanf ("%d%d", &n,  &m)!=eof)    {for        (i=1; i<=n; i++)        {            scanf ("%d%d", &c[i] , &w[i]);        }        memset (f, 0, sizeof (f));        for (j=1; j<=n; j + +)        {for            (k=0; k<=m; k++) {                if (K>=c[j])                {                    F[k]=max (f[k], f[ K-C[J]]+W[J]);        }}} printf ("%d\n", F[m]);    }    return 0;}

Hihocoder #1039: Character elimination (String processing Class) A long time ago to do the topic, the specific algorithm code read it

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.