ZOJ 3983 Crusaders Quest

来源:互联网 发布:php在windows和linux 编辑:程序博客网 时间:2024/06/06 01:30

Description

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 k(k>=1) 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 (k=3)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 T(about 50), indicating the number of test cases. For each test case:

The first line contains a string s(absolute(s) = 9)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
7gggaaaoooaaoogggoagooggaaaoagogaooaggoooggaaagogogoaaagaogaogao
Sample Output
3321321
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.


题目大意

给定一个长度为9的只含有‘a’, ‘g’, ‘o’字符的字符串, 如果遇到连续三个字符相同的则可以消除, 如果没有则可以指定一种字符将其消除, 输出最多能消除几次连续三个字符的字符串。

解题思路

因为只有三中字符且长度为9, 我们上来可以直接判断是否可以三个三个直接将字符串消完, 对于省剩个字符的情况直接输出3,剩6个的情况因为一定会消除一种字符而得到另外一种字符的连续(因为一共只有三种)直接输出2, 对于最后一种情况就是剩下9个(一种也没消除), 我们枚举每一种情况, 对于删除a, 删除o, 删除g的三种情况依次讨论选最大即可。(我在这里解释, 比如只删除a, 那么这个字符串里就只剩下o,g, 那么现在只剩下两种情况, 可以直接消除完剩下两种或是再消除一种剩下最后一种字符,答案分别是2和1. 结论一样可以推广到o, g).

代码

#include <iostream>#include <sstream>#include <cstdio>#include <algorithm>#include <cstring>#include <iomanip>#include <utility>#include <string>#include <cmath>#include <vector>#include <bitset>#include <stack>#include <queue>#include <deque>#include <map>#include <set>using namespace std;/* *ios::sync_with_stdio(false); */typedef long long ll;typedef unsigned long long ull;const int inf = 0x7fffffff;const int mod = 1000000;const int Max = (int) 2e5 + 9;string str;int solve() {    string temp = str, t;    int ans = 0, tt = 0, ttt = 0;    if (str.find("ooo") != -1 && str.find("ggg") != -1 && str.find("aaa") != -1)        return 3;    while (temp.find("ooo") != -1 || temp.find("ggg") != -1 || temp.find("aaa") != -1) {        if (temp.find("ooo") != -1) {            temp.erase(temp.find("ooo"), 3);            ttt++;        }        if (temp.find("aaa") != -1) {            temp.erase(temp.find("aaa"), 3);            ttt++;        }        if (temp.find("ggg") != -1) {            temp.erase(temp.find("ggg"), 3);            ttt++;        }    }    // cout << temp.size() << endl;    if (temp.size() == 0) return 3;    if (temp.size() == 6) return 2;    temp = str;    // delete a    while (temp.find('a') != -1) {        temp.erase(temp.find('a'), 1);    }    if (temp.find("ooo") != -1 || temp.find("ggg") != -1) {        tt = 2;    }    else {        tt = 1;    }    ans = max(ans, tt);    temp = str;    // delete o    while (temp.find('o') != -1) {        temp.erase(temp.find('o'), 1);    }    if (temp.find("aaa") != -1 || temp.find("ggg") != -1) {        tt = 2;    }    else {        tt = 1;    }    ans = max(ans, tt);    temp = str;    // delete g    while (temp.find('g') != -1) {        temp.erase(temp.find('g'), 1);    }    if (temp.find("aaa") != -1 || temp.find("ooo") != -1) {        tt = 2;    }    else {        tt = 1;    }    return max(ans, tt);}int main() {    int t;    ios::sync_with_stdio(false);    // freopen("input.txt", "r", stdin);    while (cin >> t) {        while (t--) {            cin >> str;            cout << solve() << endl;        }    }    return 0;}

原创粉丝点击