usaco5.5.3 Two Five
来源:互联网 发布:手机网站seo 编辑:程序博客网 时间:2024/06/09 13:27
一 原题
IOI 2001
In order to teach her young calvess the order of the letters in the alphabet, Bessie has come up with a game to play with them. The calves are given a 5 x 5 grid on which they can put the letters 'A'-'Y', but the one rule is that all the letters going across the columns and down the rows must be in the order they appear in the alphabet.
There are a huge number of possible grids, so Bessie decides that they will be named by the string of letters that is given by reading across each row, going down the rows. For example, the grid:
A B C D E F G H I J K L M N O P Q R S T U V W X Y
would have the name ABCDEFGHIJKLMNOPQRSTUVWXY, which is coincidentally the first possible grid when the entire set of grids is ordered alphabetically. The second grid that meets this requirement is ABCDEFGHIJKLMNOPQRSUTVWXY, which is formed by switching the 'T' and 'U' in the above grid.
Help the calves gain bragging rights. Given a number, M, find which string is Mth in the list of all possible grids when they are sorted alphabetically, and, given a string of letters, find out what number the corresponding grid is in the list of all possible grids.
PROGRAM NAME: twofive
INPUT FORMAT
The first input line contains one of two letters, either an 'N' or a 'W'.
If the first input line contains an 'N', the second line will contain an integer, M, that corresponds to the number of a valid grid. If the first line contains a 'W', the second line will contain a string of 25 letters, which represents a valid grid.
OUTPUT FORMAT
If the input contained the number of a valid grid (first line 'N'), the output should contain a string of 25 letters on a line, which corresponds to the Mth grid in the sorted list of all possible grids.
If the input contained a string of letters indicating a grid (first line 'W'), the output should contain a single integer on a line, which corresponds to the number of the given grid in the list of all possible grids.
SAMPLE INPUT #1 (file twofive.in)
N2
SAMPLE OUTPUT #1 (file twofive.out)
ABCDEFGHIJKLMNOPQRSUTVWXY
SAMPLE INPUT #2 (file twofive.in)
WABCDEFGHIJKLMNOPQRSUTVWXY
SAMPLE OUTPUT #2 (file twofive.out)
2
二 分析
三 代码
USER: Qi Shen [maxkibb3]TASK: twofiveLANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.000 secs, 4216 KB] Test 2: TEST OK [0.000 secs, 4216 KB] Test 3: TEST OK [0.000 secs, 4216 KB] Test 4: TEST OK [0.000 secs, 4216 KB] Test 5: TEST OK [0.000 secs, 4216 KB] Test 6: TEST OK [0.000 secs, 4216 KB] Test 7: TEST OK [0.000 secs, 4216 KB] Test 8: TEST OK [0.000 secs, 4216 KB] Test 9: TEST OK [0.000 secs, 4216 KB] Test 10: TEST OK [0.000 secs, 4216 KB] Test 11: TEST OK [0.000 secs, 4216 KB] Test 12: TEST OK [0.000 secs, 4216 KB]All tests OK.Your program ('twofive') produced all correct answers! This is yoursubmission #4 for this problem. Congratulations!
/*ID:maxkibb3LANG:C++PROB:twofive*/#include<cstdio>#include<cstring>char Str[30];int Appear[30];int Table[6][6][6][6][6];inline bool ok(int _l, int _c, int _upc) { if(_c == 5) return false; if(Str[_l * 5 + _c] != '\0') return false; if(_c + 1 > _upc) return false; return true;}int dfs(int _a, int _b, int _c, int _d, int _e) { if(Table[_a][_b][_c][_d][_e] != 0) return Table[_a][_b][_c][_d][_e]; int id = _a + _b + _c + _d + _e; if(id == 25) return Table[5][5][5][5][5] = 1; int ret = 0; if(Appear[id] != -1) { int line = Appear[id] / 5; if(line == 0) ret = dfs(_a + 1, _b, _c, _d, _e); else if(line == 1) ret = dfs(_a, _b + 1, _c, _d, _e); else if(line == 2) ret = dfs(_a, _b, _c + 1, _d, _e); else if(line == 3) ret = dfs(_a, _b, _c, _d + 1, _e); else if(line == 4) ret = dfs(_a, _b, _c, _d, _e + 1); } else { if(ok(0, _a, 5)) ret += dfs(_a + 1, _b, _c, _d, _e); if(ok(1, _b, _a)) ret += dfs(_a, _b + 1, _c, _d, _e); if(ok(2, _c, _b)) ret += dfs(_a, _b, _c + 1, _d, _e); if(ok(3, _d, _c)) ret += dfs(_a, _b, _c, _d + 1, _e); if(ok(4, _e, _d)) ret += dfs(_a, _b, _c, _d, _e + 1); } return Table[_a][_b][_c][_d][_e] = ret;}int main() { freopen("twofive.in", "r", stdin); freopen("twofive.out", "w", stdout); char c, s[30], ans_s[30] = {'\0'}; int n, ans_n = 0; scanf("%c", &c); if(c == 'W') { scanf("%s", s); memset(Appear, -1, sizeof(Appear)); for(int i = 0; i < 25; i++) { for(Str[i] = 'A'; Str[i] < s[i]; Str[i]++) { if(Appear[Str[i] - 'A'] != -1) continue; if(i % 5 != 0 && Str[i] < Str[i - 1]) continue; if(i / 5 != 0 && Str[i] < Str[i - 5]) continue; Appear[Str[i] - 'A'] = i; memset(Table, 0, sizeof(Table)); ans_n += dfs(0, 0, 0, 0, 0); Appear[Str[i] - 'A'] = -1; } Appear[Str[i] - 'A'] = i; } printf("%d\n", ++ans_n); } else { scanf("%d", &n); memset(Appear, -1, sizeof(Appear)); for(int i = 0; i < 25; i++) { for(Str[i] = 'A';; Str[i]++) { if(Appear[Str[i] - 'A'] != -1) continue; if(i % 5 != 0 && Str[i] < Str[i - 1]) continue; if(i / 5 != 0 && Str[i] < Str[i - 5]) continue; Appear[Str[i] - 'A'] = i; memset(Table, 0, sizeof(Table)); int tmp = dfs(0, 0, 0, 0, 0); if(tmp >= n) { ans_s[i] = Str[i]; break; } n -= tmp; Appear[Str[i] - 'A'] = -1; } } printf("%s\n", ans_s); } return 0;}
// better-written code
/*ID:maxkibb3LANG:C++PROB:twofive*/#include<cstdio>#include<cstring>char Prefix[26];int F[6][6][6][6][6];bool ok(int _x, int _dep) { return !Prefix[_x] || Prefix[_x] == _dep + 'A';}int dfs(int _a, int _b, int _c, int _d, int _e, int _dep) { if(_dep == 25) return 1; int ret = 0, tmp = F[_a][_b][_c][_d][_e]; if(tmp) return tmp; if(_a < 5 && ok(_a, _dep)) ret += dfs(_a + 1, _b, _c, _d, _e, _dep + 1); if(_b < _a && ok(_b + 5, _dep)) ret += dfs(_a, _b + 1, _c, _d, _e, _dep + 1); if(_c < _b && ok(_c + 10, _dep)) ret += dfs(_a, _b, _c + 1, _d, _e, _dep + 1); if(_d < _c && ok(_d + 15, _dep)) ret += dfs(_a, _b, _c, _d + 1, _e, _dep + 1); if(_e < _d && ok(_e + 20, _dep)) ret += dfs(_a, _b, _c, _d, _e + 1, _dep + 1); return F[_a][_b][_c][_d][_e] = ret;}int main() { freopen("twofive.in", "r", stdin); freopen("twofive.out", "w", stdout); char c, s[26], ans_s[26] = {'\0'}; int n, ans_n = 0; scanf("%c", &c); if(c == 'W') { scanf("%s", s); for(int i = 0; i < 25; i++) { for(Prefix[i] = 'A'; Prefix[i] < s[i]; Prefix[i]++) { memset(F, 0, sizeof(F)); ans_n += dfs(0, 0, 0, 0, 0, 0); } } printf("%d\n", ans_n + 1); } else { scanf("%d", &n); for(int i = 0; i < 25; i++) { for(Prefix[i] = 'A';; Prefix[i]++) { memset(F, 0, sizeof(F)); int tmp = dfs(0, 0, 0, 0, 0, 0); if(tmp >= n) break; else n -= tmp; } ans_s[i] = Prefix[i]; } printf("%s\n", ans_s); } return 0;}
- usaco5.5.3 Two Five
- P2750 [USACO5.5]贰五语言Two Five
- 洛谷P2750 [USACO5.5]贰五语言Two Five (dfs + 记忆化 + 逼近)
- USACO5.5.3 (twofive)
- [USACO5.5.3]Twofive
- Five
- five
- Lesson one hundred twenty five Tea for two.
- 如何实现将'one,two,three,four,five' 分别打印
- HDU 3131 One…Two…Five! (暴力搜索)
- 小学三年级题目 five+one+two=eight
- CodeVS 1416|USACO Train 5.5.3|Two Five|二五语言|搜索
- chapter five
- Five 'W'
- five minutes
- unit five
- Luna Five
- five继承
- d3入门地址
- 图解Java之二——List
- 查询出部门名称、部门的员工数、部门的平均工资、部门的最低收入雇员姓名和最高收入雇员的姓名
- banner轮播
- Windows平台安装 MongoDB
- usaco5.5.3 Two Five
- 自动横屏切换
- AES加解密-IOS与ANR对比
- 【C++】string的 [ ] 的两种使用方法
- intellij Idea创建Web项目
- 模糊查询
- android java语言实现的帧动画
- JSONP和Ajax的区别
- 基于Glib的网络实现示例