您的位置:首页 > 编程语言 > Go语言

LightOJ 1051 Good or Bad 解题报告

2015-08-04 14:00 507 查看
Description

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

思路:
f1[i][j]=1表示串p[i]可以以j个元音字母结尾;

f2[i][j]=1表示串p[i]可以以j个辅音字母结尾。

所以转移公式:

(1)f1[i-1][j]=1,则若s[i]为元音或问号,f1[i][j+1]=1;若s[i] 为辅音或者问号,则f2[i][1]=1;

(2)f2[i-1][j]=1,则若s[i]为辅音或问号,f2[i][j+1]=1;若s[i] 为元音或者问号,则f1[i][1]=1。
详见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int n,a[55],f1[55][4],f2[55][6];//a用来存字母状态,1为辅音,0为元音,2为问号
char s[55];

int change(char c)
{
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U') return 1;
else return 0;
}

void dp()
{
memset(f1,0,sizeof(f1));
memset(f2,0,sizeof(f2));
f1[0][0]=f2[0][0]=1;//前0个后缀0都是合法的,初始为1
for(int i=1;i<=n;i++)//每个字符过一遍
{
for(int j=0;j<=2;j++)//为什只遍历到2,因为元音只要3个就bad了,后缀再多也是bad
if(f1[i-1][j])//如果前i-1个后缀是j个元音,那若这个是元音,f1[i][j+1]=1就可以
{
if(a[i]==2||a[i]==1) f2[i][1]=1;//已经j个元音结尾(j未到3,不满足bad时),若当前是辅音,可以以辅音当前开头
if(a[i]==2||a[i]==0) f1[i][j+1]=1;
}
for(int j=0;j<=4;j++)//元音同上
if(f2[i-1][j])
{
if(a[i]==2||a[i]==0) f1[i][1]=1;
if(a[i]==2||a[i]==1) f2[i][j+1]=1;
}
}
int bad=0,good=0;
for(int i=0;i<=2;i++)//只要有一个符合good就为1。因为列举了所有情况,出现一种符合即是good可能
if(f1
[i]) good=1;
for(int i=0;i<=4;i++)
if(f2
[i]) good=1;
for(int i=1;i<=n;i++)
if(f1[i][3]||f2[i][5]) bad=1;
if(good&&bad) printf("MIXED\n");
else if(good) printf("GOOD\n");
else printf("BAD\n");
}

int main()
{
int T;
scanf("%d",&T);
for(int qq=1;qq<=T;qq++)
{
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++)
{
if(s[i]=='?') a[i]=2;
else if(change(s[i])) a[i]=0;
else a[i]=1;
}
printf("Case %d: ",qq);
dp();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: