LightOJ 1051 - Good or Bad (dp)
2015-11-20 17:00
525 查看
题意:
给定一个字符串,连续3个元音字母或者连续5个辅音字母为BAD,如果完全不BAD为GOOD,"?"可以是任意字母,如果都有可能是MIXED
分析:
dp[i][j][k]:=第i个字母,连续j个元音或者连续k个辅音,的状态
转移就很蛋疼了−−反正最后ac了
代码:
分析:
看了下大牛写的,dp[i][j][k]:=此状态是否可达,问题一下就简单了−−
代码:
给定一个字符串,连续3个元音字母或者连续5个辅音字母为BAD,如果完全不BAD为GOOD,"?"可以是任意字母,如果都有可能是MIXED
分析:
dp[i][j][k]:=第i个字母,连续j个元音或者连续k个辅音,的状态
转移就很蛋疼了−−反正最后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 [i][0]) continue; if(dp [i][0] == 1) good = true; else if(dp [i][0] == 2) bad = true; else return 3; } for(int i = 1; i <= 5; ++i) { if(!dp [0][i]) continue; if(dp [0][i] == 1) good = true; else if(dp [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 [i][0]) good = true; for(int i = 1; i < 5; ++i) if(dp [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; }
相关文章推荐
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题
- LeetCode之Maximum Product Subarray
- DP Flow