Codeforecs 65D. Harry Potter and the Sorting Hat 一种新的记忆化搜索方式
来源:互联网 发布:订单源码 编辑:程序博客网 时间:2024/06/04 18:38
As you know, Hogwarts has four houses: Gryffindor, Hufflepuff, Ravenclaw and Slytherin. The sorting of the first-years into houses is done by the Sorting Hat. The pupils are called one by one in the alphabetical order, each of them should put a hat on his head and, after some thought, the hat solemnly announces the name of the house the student should enter.
At that the Hat is believed to base its considerations on the student's personal qualities: it sends the brave and noble ones to Gryffindor, the smart and shrewd ones — to Ravenclaw, the persistent and honest ones — to Hufflepuff and the clever and cunning ones — to Slytherin. However, a first year student Hermione Granger got very concerned about the forthcoming sorting. She studied all the literature on the Sorting Hat and came to the conclusion that it is much simpler than that. If the relatives of the student have already studied at Hogwarts, the hat puts the student to the same house, where his family used to study. In controversial situations, when the relatives studied in different houses or when they were all Muggles like Hermione's parents, then the Hat sorts the student to the house, to which the least number of first years has been sent at that moment. If there are several such houses, the choice is given to the student himself. Then the student can choose any of the houses, to which the least number of first years has been sent so far.
Hermione has already asked the students that are on the list before her about their relatives. Now she and her new friends Harry Potter and Ron Weasley want to find out into what house the Hat will put Hermione.
The first input line contains an integer n (1 ≤ n ≤ 10000). It is the number of students who are in the list before Hermione. The next line contains n symbols. If all the relatives of a student used to study in the same house, then the i-th character in the string coincides with the first letter of the name of this house. Otherwise, the i-th symbol is equal to "?".
Print all the possible houses where Hermione can be sent. The names of the houses should be printed in the alphabetical order, one per line.
11G????SS???H
GryffindorRavenclaw
2H?
GryffindorRavenclawSlytherin
Consider the second example. There are only two students before Hermione. The first student is sent to Hufflepuff. The second disciple is given the choice between the houses where the least number of students has been sent, i.e. Gryffindor, Slytherin and Ravenclaw. If he chooses Gryffindor, Hermione is forced to choose between Ravenclaw and Slytherin, if he chooses Ravenclaw, Hermione will choose between Gryffindor and Slytherin, if he chooses Slytherin, Hermione will choose between Gryffindor and Ravenclaw. In the end, the following situation is possible (it depends on the choice of the second student and Hermione). Hermione will end up 1) in Gryffindor, 2) in Ravenclaw, 3) in Slytherin. Note that, despite the fact that in neither case Hermione will be given a choice between all the three options, they are all possible and they should all be printed in the answer. Hermione will not, under any circumstances, end up in Hufflepuff.
将4维参数压入一个vector中,利用set判断该状态是否出现过。
#include <iostream>#include <string>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <map>#include <set>#include <vector>#include <queue>#include <stack>using namespace std;#define max_int INT_MAX / 2#define max_long 0xFFFFFFFFFFFFFFFLL / 2#define two(a) (1 << (a))#define eps 1e-6#define FF(i, a, b) for (int i = (a); i <= (b); i++)#define FFD(i, a, b) for (int i = (a); i >= (b); i--)const int OO=1e9;const int INF=1e9;int n;char s[11111];char name[4][50]={"Gryffindor","Hufflepuff","Ravenclaw","Slytherin"};set< vector<int> >vis;bool ans[5]={0};void dfs(int deep, vector<int>v ){ if (vis.find(v)!=vis.end()) return; vis.insert(v); int min_s=*min_element(v.begin(),v.end()); if (deep==n) { for (int i=0;i<4;i++) { if (v[i]==min_s) { ans[i]=true; } } } else if (s[deep]=='?') { for (int i=0;i<4;i++) { if (v[i]==min_s) { v[i]++; dfs(deep+1,v); v[i]--; } } } else { if (s[deep]=='G') { v[0]++; dfs(deep+1,v); } if (s[deep]=='H') { v[1]++; dfs(deep+1,v); } if (s[deep]=='R') { v[2]++; dfs(deep+1,v); } if (s[deep]=='S') { v[3]++; dfs(deep+1,v); } }}int main(){ cin>>n; cin>>s; dfs(0,vector<int>(4,0)); for (int i=0;i<4;i++) { if (ans[i]) { cout<<name[i]<<endl; } } return 0;}
- Codeforecs 65D. Harry Potter and the Sorting Hat 一种新的记忆化搜索方式
- Codeforce 65D - Harry Potter and the Sorting Hat(DP+hash+set)
- HDU 3985 Harry Potter and the D.A.
- Harry Potter and the Deathly Hallows
- hdu3986 Harry Potter and the Final Battle
- HDU3988 Harry Potter and the Hide Story
- Harry Potter and the Chamber of Secrets
- Harry Potter and the Prisoner of Azkaban
- Harry Potter and the Goblet of Fire
- Harry Potter and the Half-Blood Prince
- Harry Potter and the Deathly Hallows
- hdu3991 Harry Potter and the Present II
- Harry Potter and The Sorcerer's stone
- Harry Potter and the Hide Story HDU
- ACM题“Harry Potter and His Magic Scroll”的一种解法
- Harry Potter and the Order of the Phoenix
- Harry Potter and the Order of the Phoenix
- HDU--3987[Harry Potter and the Forbidden Forest] 求最小割集中的最小边数
- Ubuntu 12.10 安装 Eclipse
- C++ 实现简单的静态队列
- J2ME的List、TextBox,Ticker和Alert
- 关于System.arraycopy方法的使用
- Java乔晓松-oracle的多表查询
- Codeforecs 65D. Harry Potter and the Sorting Hat 一种新的记忆化搜索方式
- 安卓-开发工具之DDMS
- 切割日志的方法
- 数位dp学习小记 Hdu 3555
- codeforces 288,289(Round #177)题解
- 如何成为一个优秀的JAVA程序员 如何成为高级JAVA程序员 细节决定一个程序员的成败
- 发送 handle 消息 更新UI
- java方面的书籍
- [DirectShow] 枚举设备到Combo Box列表中 [大三TJB_708]