LightOJ 1051 Good or Bad 解题报告

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[n][i]) good=1;
    for(int i=0;i<=4;i++)
        if(f2[n][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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值