lightoj1051_dp

本文提供了一种解决LightOJ 1051问题的有效算法,该问题涉及判断字符串是否包含特定模式。通过使用动态规划方法,文章详细解释了如何评估字符串并确定其是否符合题目定义的好或坏标准。

http://lightoj.com/volume_showproblem.php?problem=1051

1051 - Good or Bad
Time Limit: 2 second(s)Memory Limit: 32 MB

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

 

 

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <vector>
 7 #include <ctime>
 8 #include <queue>
 9 #include <list>
10 #include <set>
11 #include <map>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 typedef long long LL;
15 
16 int dp[55][10][10]; //dp[i][j][k],表示以第i位为结尾,元音个数为j或辅音个数为k的情况是否存在
17 char s[55];
18 int main()
19 {
20     int t;
21     scanf("%d", &t);
22     for(int ca = 1; ca <= t; ca++)
23     {
24         scanf("%s", s+1);
25         int len = strlen(s+1);
26         memset(dp, 0, sizeof(dp));
27         dp[0][0][0] = 1;
28         int Bad = 0, Good = 0;
29         for(int i = 1; i <= len; i++)
30         {
31             for(int j = 0; j < 3; j++)
32             {
33                 if(dp[i-1][j][0] == 0)
34                     continue;
35                 if(s[i] == '?')
36                     dp[i][j+1][0] = dp[i][0][1] = 1;
37                 else if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
38                     dp[i][j+1][0] = 1;
39                 else
40                     dp[i][0][1] = 1;
41             }
42             if(dp[i][3][0])
43                 Bad = 1;
44             for(int j = 0; j < 5; j++)
45             {
46                 if(dp[i-1][0][j] == 0)
47                     continue;
48                 if(s[i] == '?')
49                     dp[i][0][j+1] = dp[i][1][0] = 1;
50                 else if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
51                     dp[i][1][0] = 1;
52                 else
53                     dp[i][0][j+1] = 1;
54             }
55             if(dp[i][0][5])
56                 Bad = 1;
57         }
58         for(int i = 1; i < 3; i++)
59             if(dp[len][i][0])
60                 Good = 1;
61         for(int i = 1; i < 5; i++)
62             if(dp[len][0][i])
63                 Good = 1;
64         printf("Case %d: ", ca);
65         if(Bad && Good)
66             puts("MIXED");
67         else if(Bad)
68             puts("BAD");
69         else
70             puts("GOOD");
71     }
72     return 0;
73 }

 

转载于:https://www.cnblogs.com/luomi/p/5955815.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值