ZOJ

来源:互联网 发布:软件开发职位 编辑:程序博客网 时间:2024/06/18 15:37

Crusaders Quest

Time Limit: 1 Second      Memory Limit: 65536 KB

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

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.


题意:有一串字符串只有a b c 三种字母,代表三个技能,每次可以消除一段连续且相同的字母,或单独一个字母,消除后字符串两端的字母会合拢,当消除的字母块为三个相同字母时,就会有一个super skill ,问最多会有几个super skill , 字符串长度固定为9,且abc三种字符各自出现了三次


因为状态比较少,所以可以直接来dfs一下就可以了


#include <iostream>#include <string.h>#include <stdio.h>using namespace std;const int N = 1e2+10;int T,ans = 0;char s[N];int vis[2][2][2][2][2][2][2][2][2][5], use[N];void dfs(int kill){    ans = max(ans, kill);    if(vis[use[0]][use[1]][use[2]][use[3]][use[4]][use[5]][use[6]][use[7]][use[8]][kill]){        return;    }    vis[use[0]][use[1]][use[2]][use[3]][use[4]][use[5]][use[6]][use[7]][use[8]][kill] = 1;    for(int i=0;i<9;i++){        if(!use[i]){            int l = 0 , r = 0,L[5],R[5];            for(int j=1;j<9&&i+j<9;j++){                if(use[i+j]) continue;                if(s[i+j]!=s[i]) break;                R[r++] = i+j;                if(r==2) break;            }            for(int j=1;j<9&&i-j>=0;j++){                if(use[i-j]) continue;                if(s[i-j]!=s[i]) break;                L[l++] = i-j;                if(l==2) break;            }            if(l+r>=2){                if(l>=2){                    use[i] = use[L[0]] = use[L[1]] = 1;                    dfs(kill+1);                    use[i] = use[L[0]] = use[L[1]] = 0;                }else if(r>=2){                    use[i] = use[R[0]] = use[R[1]] = 1;                    dfs(kill+1);                    use[i] = use[R[0]] = use[R[1]] = 0;                }else{                    use[i] = use[L[0]] = use[R[0]] = 1;                    dfs(kill+1);                    use[i] = use[L[0]] = use[R[0]] = 0;                }            }else{                if(l+r==0){                    use[i] = 1;                    dfs(kill);                    use[i] = 0;                }else{                    if(l==1){                        use[i] = use[L[0]] = 1;                        dfs(kill);                        use[i] = use[L[0]] = 0;                    }else{                        use[i] = use[R[0]] = 1;                        dfs(kill);                        use[i] = use[R[0]] = 0;                    }                }            }        }    }}int main(){    scanf("%d",&T);    while(T--){        memset(vis, 0, sizeof vis);        memset(use, 0, sizeof use);        ans = 0;        scanf("%s",s);        dfs(0);        printf("%d\n",ans);    }        return 0;}



原创粉丝点击