Good or Bad LightOJ - 1051
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
5
FFFF?EE
HELLOWORLD
ABCDEFGHIJKLMNOPQRSTUVWXYZ
HELLO?ORLD
AAA
Sample Output
Case 1: BAD
Case 2: GOOD
Case 3: BAD
Case 4: MIXED
Case 5: BAD
詳解:http://www.cnblogs.com/jianglangcaijin/archive/2012/10/12/2721156.html
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4#include<algorithm> 5 using namespace std; 6 7 int n; 8 int a[60],d1[60][6],d2[60][6]; 9 char S[60]; 10 11 int OK(char x){ 12 return x==‘A‘||x==‘E‘||x==‘I‘||x==‘O‘||x==‘U‘; 13 } 14 15 void Inite(){ 16 memset(d1,0,sizeof(d1)); 17 memset(d2,0,sizeof(d2)); 18 d1[0][0]=d2[0][0]=1; 19 for(int i=1;i<=n;i++){ 20 if(S[i]==‘?‘) a[i]=2; 21 else if(OK(S[i])) a[i]=0; 22 else a[i]=1; 23 } 24 } 25 26 void solve(){ 27 for(int i=1;i<=n;i++){ 28 for(int j=0;j<=2;j++) if(d1[i-1][j]) 29 { 30 if(a[i]==2||a[i]==1) d2[i][1]=1; 31 if(a[i]==2||a[i]==0) d1[i][j+1]=1; 32 } 33 for(int j=0;j<=4;j++) if(d2[i-1][j]) 34 { 35 if(a[i]==2||a[i]==0) d1[i][1]=1; 36 if(a[i]==2||a[i]==1) d2[i][j+1]=1; 37 } 38 } 39 40 int bad=0,good=0; 41 for(int i=0;i<=2;i++) if(d1[n][i]) good=1; 42 for(int i=0;i<=4;i++) if(d2[n][i]) good=1; 43 for(int i=1;i<=n;i++) if(d1[i][3]||d2[i][5]) bad=1; 44 if(good&&bad) puts("MIXED"); 45 else if(good) puts("GOOD"); 46 else puts("BAD"); 47 } 48 49 int main() 50 { int kase; 51 cin>>kase; 52 for(int t=1;t<=kase;t++){ 53 scanf("%s",S+1); 54 n=strlen(S+1); 55 printf("Case %d: ",t); 56 Inite(); 57 solve(); 58 } 59 return 0; 60 }
Good or Bad LightOJ - 1051