1051 - Good or Bad

A string is called bad if it has 3 vowels in a row, or 5 consonants in a row, or both. A string is called good if it is not bad. You are given a string s, consisting of uppercase letters ('A'-'Z') and question marks('?'). Return "BAD" if the string is definitely bad (that means you cannot substitute letters for question marks so that the string becomes good), "GOOD" if the string is definitely good, and "MIXED" if it can be either bad or good.

The letters 'A', 'E', 'I', 'O', 'U' are vowels, and all others are consonants.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case begins with a non-empty string with length no more than 50.

Output

For each case of input you have to print the case number and the result according to the description.

Sample Input

Output for Sample Input

5

FFFF?EE

HELLOWORLD

ABCDEFGHIJKLMNOPQRSTUVWXYZ

HELLO?ORLD

AAA

Case 1: BAD

Case 2: GOOD

Case 3: BAD

Case 4: MIXED

Case 5: BAD

 

 

一道容易想但是边界另人蛋疼的DP。

DP[I][J]表示到某一位,与末尾相连的元音个数为i,辅音个数为j的情况,如果dp[i][j]==0,说明该情况不存在,1表示存在。递推一次就行,注意细节。

此题还有一个要注意的地方,就是要用滚动数组,直接开到3维会爆内存,而且初始化很浪费时间。

滚动数组:当dp的某一个状态仅个上一个状态有关系时,可以使用滚动数组。dp[0],dp[1]轮流使用,巧妙使用%或者&可优化代码。

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 bool flag[30]={0};
 6 int main(){
 7     flag[0]=flag[4]=flag[8]=flag[14]=flag[20]=1;
 8     int t,cas=1;
 9     cin>>t;
10     while(t--){
11         char mas[55];
12         scanf("%s",mas+1);
13         int n=strlen(mas+1);
14         bool a=0,b=0;
15         bool dp[2][4][6]={0};
16         dp[0][0][0]=1;
17         for(int i=1;i<=n;i++){
18             memset(dp[i%2],0,sizeof(dp[i%2]));
19             for(int j=0;j<3;j++){
20                 if(!dp[(i+1)%2][j][0]) continue;
21                 if(mas[i]=='?') dp[i%2][j+1][0]=dp[i%2][0][1]=1;
22                 else if(flag[mas[i]-'A']) dp[i%2][j+1][0]=1;
23                 else dp[i%2][0][1]=1;
24             }
25             for(int j=0;j<5;j++){
26                 if(!dp[(i+1)%2][0][j]) continue;
27                 if(mas[i]=='?') dp[i%2][0][j+1]=dp[i%2][1][0]=1;
28                 else if(flag[mas[i]-'A']) dp[i%2][1][0]=1;
29                 else dp[i%2][0][j+1]=1;
30             }
31             if(dp[i%2][3][0] || dp[i%2][0][5]) a=1;
32         }
33         for(int i=0;i<3;i++) if(dp[n%2][i][0]) b=1;
34         for(int i=0;i<5;i++) if(dp[n%2][0][i]) b=1;
35         printf("Case %d: ",cas++);
36         if(a==1 && b==1) puts("MIXED");
37         else if(b) puts("GOOD");
38         else puts("BAD");
39     }
40     return 0;
41 }

 

posted on 2013-04-03 22:39  SCAU_Xxm  阅读(342)  评论(0)    收藏  举报