LightOJ 1051 - Good or Bad (dp)

原创 2015年11月20日 17:00:19

题意:

,35BAD,BADGOOD,"?",MIXED

分析:

dp[i][j][k]:=i,jk,
ac

代码:

//
//  Created by TaoSama on 2015-11-20
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, dp[55][4][6]; //0Illegal 1GOOD 2BAD 3MIXED
char s[55];

bool isVowel(char c) {
    return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
}

void get(int&x, int y) {
    if(x && x != y) x = 3;
    else x = y;
}

//1GOOD 2BAD 3MIXED
int gao() {
    bool good = false, bad = false;
    for(int i = 1; i <= 3; ++i) {
        if(!dp[n][i][0]) continue;
        if(dp[n][i][0] == 1) good = true;
        else if(dp[n][i][0] == 2) bad = true;
        else return 3;
    }
    for(int i = 1; i <= 5; ++i) {
        if(!dp[n][0][i]) continue;
        if(dp[n][0][i] == 1) good = true;
        else if(dp[n][0][i] == 2) bad = true;
        else return 3;
    }
    if(good && bad) return 3;
    else if(good) return 1;
    else return 2;
}

int main() {
#ifdef LOCAL
    freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    int t; scanf("%d", &t);
    int kase = 0;
    while(t--) {
        scanf("%s", s + 1);
        n = strlen(s + 1);
        memset(dp, 0, sizeof dp);
        if(s[1] == '?' || isVowel(s[1])) dp[1][1][0] = 1;
        if(s[1] == '?' || !isVowel(s[1])) dp[1][0][1] = 1;
        for(int i = 1; i < n; ++i) {
            for(int j = 1; j <= 3; ++j) {
                if(!dp[i][j][0]) continue;
                if(s[i + 1] == '?' || isVowel(s[i + 1])) {
                    if(j >= 2) dp[i + 1][3][0] = 2;
                    else get(dp[i + 1][j + 1][0], dp[i][j][0]);
                }
                if(s[i + 1] == '?' || !isVowel(s[i + 1]))
                    get(dp[i + 1][0][1], dp[i][j][0]);
            }
            for(int j = 1; j <= 5; ++j) {
                if(!dp[i][0][j]) continue;
                if(s[i + 1] == '?' || !isVowel(s[i + 1])) {
                    if(j >= 4) dp[i + 1][0][5] = 2;
                    else get(dp[i + 1][0][j + 1], dp[i][0][j]);
                }
                if(s[i + 1] == '?' || isVowel(s[i + 1]))
                    get(dp[i + 1][1][0], dp[i][0][j]);
            }
        }
        int ans = gao();
        string x = ans == 1 ? "GOOD" : ans == 2 ? "BAD" : "MIXED";
        printf("Case %d: %s\n", ++kase, x.c_str());
    }
    return 0;
}

分析:
,dp[i][j][k]:=,

代码:

//
//  Created by TaoSama on 2015-11-20
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n;
bool dp[55][4][6];
char s[55];

bool isVowel(char c) {
    return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
}

int main() {
#ifdef LOCAL
    freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    int t; scanf("%d", &t);
    int kase = 0;
    while(t--) {
        scanf("%s", s + 1);
        n = strlen(s + 1);
        memset(dp, false, sizeof dp);
        dp[0][0][0] = true;
        for(int i = 1; i <= n; ++i) {
            for(int j = 0; j < 3; ++j) {
                if(s[i] == '?' || isVowel(s[i]))
                    dp[i][j + 1][0] |= dp[i - 1][j][0];
                if(s[i] == '?' || !isVowel(s[i]))
                    dp[i][0][1] |= dp[i - 1][j][0];
            }
            for(int j = 0; j < 5; ++j) {
                if(s[i] == '?' || !isVowel(s[i]))
                    dp[i][0][j + 1] |= dp[i - 1][0][j];
                if(s[i] == '?' || isVowel(s[i]))
                    dp[i][1][0] |= dp[i - 1][0][j];
            }
        }
        bool good = false, bad = false;
        for(int i = 1; i < 3; ++i) if(dp[n][i][0]) good = true;
        for(int i = 1; i < 5; ++i) if(dp[n][0][i]) good = true;
        for(int i = 1; i <= n; ++i) {
            if(dp[i][3][0]) bad = true;
            if(dp[i][0][5]) bad = true;
        }
        printf("Case %d: ", ++kase);
        if(good && bad) puts("MIXED");
        else if(good) puts("GOOD");
        else puts("BAD");
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

D - Red-Green Towers dp

D - Red-Green Towers Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64...

Codeforces Round #369 (Div. 2)C. Coloring Trees

C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes ...

lightoj 1051 - Good or Bad DP

给定一个字符串,如果存在三个连续的元音字母或者五个连续的辅音字母就是bad,否则是good,但是字符串中存在?,这个可以是任何字母,如果bad和good都能到达的话就是mixed。 开始就是想着乱搞...

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. ...

Good Product Manager/Bad Product Manager 优秀的产品经理/不合格的产品经理

中文为我的y 优秀的产品经理/不合格的产品经理 ——Ben Horowitz   优秀的产品经理要求你非常的了解市场、产品、生产线以及市场的竞争,并且要求你具有丰富的知识储备和自信来扮...

Good Tech Lead, Bad Tech Lead

Good Tech Lead, Bad Tech Lead Jan 30th A brief guide to tech leadership at Foursquare, inspired ...

cin.fail(), cin.bad(), cin.good(), cin.clear(), cin.ignore()

我们经常会看到程序中会出现 cin.clear(),cin.ignore(), cin.fail()等函数。这些函数都是与cin的错误处理有关的。这一节我们来分析一下cin的错误处理机制,并且学习几个...

Lua: Good, bad, and ugly parts

Lua: Good, bad, and ugly parts from :http://notebook.kulchenko.com/programming/lua-good-different-ba...

Good vs Bad Leader

软件开发的成败更多的是在于人,而不是技术。当从大学毕业时,我以为,只有精通了各种技术才能成为一名伟大的程序员,以为人件管理技术是经理们的专属领地。但事实却给我好好的上了一堂课。经常我能听到有人说人件管...

博客推荐:good math, bad math之Lambda算子简介

博客推荐:good math, bad math之Lambda算子简介 分类: 计算机科学 阅读流水帐 翻译2006-05-22 06:54 8928人阅读 评论(2) 收藏 举报 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LightOJ 1051 - Good or Bad (dp)
举报原因:
原因补充:

(最多只允许输入30个字)