Crusaders Quest(ZOJ)

                                                Crusaders Quest

Crusaders Quest is an interesting mobile game. A mysterious witch has brought great darkness to the game world, and the only hope for your kingdom is to save the Goddesses so that they can unleash their power to fight against the witch.

In order to save the game world, you need to choose three heroes to fight for victory and use their skills wisely. Nine skill blocks of three different types (three blocks per type) will be presented at the bottom of the screen. If () consecutive blocks are of the same type, you can tap on them and eliminate them, thus triggering the powerful skill they represent. After the elimination, the blocks to their left will be connected with the blocks to their right. Moreover, if consecutive blocks of the same type are eliminated, the powerful skill they unleash will be upgraded to a super skill, which is the most powerful skill of all.

DreamGrid is a newbie in this game, and he wants to trigger the super skill as many times as he can. Given nine skill blocks satisfying the description above, please help DreamGrid calculate the maximum number of times he can trigger the super skill.

Input

There are multiple test cases. The first line of input contains an integer (about 50), indicating the number of test cases. For each test case:

The first line contains a string () consisting of three 'g's, three 'a's and three 'o's, representing the nine skill blocks of three different types. Each type of character represents one type of skill block.

Output

For each test case, output an integer denoting the maximum number of times DreamGrid can trigger the super skill.

Sample Input

7
gggaaaooo
aaoogggoa
googgaaao
agogaooag
goooggaaa
gogogoaaa
gaogaogao

Sample Output

3
3
2
1
3
2
1

Hint

For the first sample test case, DreamGrid can first eliminate "aaa" (one super skill triggered), thus changing the skill blocks to "gggooo". He can then eliminate "ggg" (another super skill triggered) and finally eliminate "ooo" (a third super skill triggered). So the answer is 3.

For the second sample test case, DreamGrid can first eliminate "ggg" (one super skill triggered), thus changing the skill blocks to "aaoooa". He can then eliminate "ooo" (another super skill triggered) and finally eliminate "aaa" (a third super skill triggered). So the answer is also 3.

For the third sample test case, DreamGrid can first eliminate "aaa" (one super skill triggered), thus changing the skill blocks to "googgo". He can then eliminate "oo" to obtain "gggo", and eliminate "ggg" (another super skill triggered) to obtain "o". So the answer is 2. It is easy to prove that he cannot trigger the super skill three times under this arrangement of skill blocks.

题目链接:

https://vjudge.net/problem/ZOJ-3983

题意描述:

给我们一个字符串 其中只有a,o,g 当三个相同字符连在一起时释放大技能,我们可以消除任意连续数量的字符,问我们最大的释放大技能的数量是多少

程序代码:

来自飞飞学长的关怀

#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int f(vector<char> v, char a, char b, char c) {
    int ans = 0;
    vector<char> tp = v;
    
    int fa = -1;
    for(int i = 0;  i < tp.size() - 2; i++) {
        if(tp[i] == a && tp[i + 1] == a && tp[i + 2] == a) {
            fa = i;
            break;
        }
    }
    if(fa != -1) {
        tp.erase(tp.begin() + fa, tp.begin() + fa + 3);
        ans++;
    }
    else {
        for(int i = 0; i < tp.size(); i++) {
            if(tp[i] == a){
                tp.erase(tp.begin() + i);
                i = 0;
            }
        }
    }
    
    int fb = -1;
    for(int i = 0;  i < tp.size() - 2; i++) {
        if(tp[i] == b && tp[i + 1] == b && tp[i + 2] == b) {
            fb = i;
            break;
        }
    }
    if(fb != -1) {
        tp.erase(tp.begin() + fb, tp.begin() + fb + 3);
        ans++;
    }
    else {
        for(int i = 0; i < tp.size(); i++) {
            if(tp[i] == b){
                tp.erase(tp.begin() + i);
                i = 0;
            }
        }
    }
    
    int fc = -1;
    for(int i = 0;  i < tp.size() - 2; i++) {
        if(tp[i] == c && tp[i + 1] == c && tp[i + 2] == c) {
            fc = i;
            break;
        }
    }
    if(fc != -1) {
        tp.erase(tp.begin() + fc, tp.begin() + fc + 3);
        ans++;
    }
    else {
        for(int i = 0; i < tp.size(); i++) {
            if(tp[i] == c){
                tp.erase(tp.begin() + i);
                i = 0;
            }
        }
    }
    
    return ans;
}

int main()
{
    int T;
    char ch;
    scanf("%d", &T);
    while(T--) {
        vector<char> v;
        vector<int> a;
        for(int i = 0; i < 9; i++) {
            scanf(" %c", &ch);
            v.push_back(ch);
        }
        a.push_back(f(v, 'g', 'a', 'o'));
        a.push_back(f(v, 'g', 'o', 'a'));
        a.push_back(f(v, 'a', 'g', 'o'));
        a.push_back(f(v, 'a', 'o', 'g'));
        a.push_back(f(v, 'o', 'g', 'a'));
        a.push_back(f(v, 'o', 'a', 'g'));
        int ans = 0;
        for(int i = 0; i < a.size(); i++) {
            ans = max(ans, a[i]);
        }
        printf("%d\n",ans);    
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/HeZhiYing_/article/details/83545409
ZOJ