https://codeforces.com/problemset/problem/65/D

哈利波特!一种新思路的状压记忆化dfs,记得每次dfs用完要减回去。而且一定是要在dfs外部进行加减!防止在中间return的时候忘记弄回来。用哈希记录状态实现真正的记忆化。

#include<bits/stdc++.h>
using namespace std;
#define ll long long int n;
char s[]; ll getID(int a[]){
ll res=;
for(int i=;i<;i++){
res=res*+a[i];
}
return res;
} int a[]={};
int c[]={}; set<ll> se; int dfs(int i){
ll ID=getID(a);
if(se.count(ID))
return ;
else{
se.insert(ID);
//printf("%d-%lld\n",i,ID);
if(i==n){
int minnum=*min_element(a,a+);
for(int j=;j<;j++){
if(a[j]==minnum){
c[j]=;
}
}
return ;
}
} if(s[i]!='?'){
switch (s[i]){
case 'G':
a[]++;
dfs(i+);
a[]--;
break;
case 'H':
a[]++;
dfs(i+);
a[]--;
break;
case 'R':
a[]++;
dfs(i+);
a[]--;
break;
case 'S':
a[]++;
dfs(i+);
a[]--;
}
}
else{
int minnum=*min_element(a,a+);
for(int j=;j<;j++){
if(a[j]==minnum){
//cout<<"j="<<j<<endl;
a[j]++;
dfs(i+);
a[j]--;
}
}
}
} int main(){
scanf("%d",&n);
scanf("%s",s);
dfs(); //printf("%d\n",se.size()); if(c[])
puts("Gryffindor");
if(c[])
puts("Hufflepuff");
if(c[])
puts("Ravenclaw");
if(c[])
puts("Slytherin"); }

Codeforces - 65D - Harry Potter and the Sorting Hat - 简单搜索的更多相关文章

  1. Timus 1446. Sorting Hat 分类问题

    At the start of each school year, a very important event happens at Hogwarts. Each of the first-year ...

  2. Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 动态规划

    C. Sorting Railway Cars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/conte ...

  3. Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS

    C. Sorting Railway Cars   An infinitely long railway has a train consisting of n cars, numbered from ...

  4. Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp

    Little Elephant and Broken Sorting 怎么感觉这个状态好难想到啊.. dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率.转移好像比较显然. #incl ...

  5. Codeforces Round #335 (Div. 2) C. Sorting Railway Cars

    C. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. CodeForces - 258D Little Elephant and Broken Sorting

    Discription The Little Elephant loves permutations of integers from 1 to n very much. But most of al ...

  7. CodeForces 258D Little Elephant and Broken Sorting(期望)

    CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...

  8. Codeforces Round #424 Div2 E. Cards Sorting

    我只能说真的看不懂题解的做法 我的做法就是线段树维护,毕竟每个数的顺序不变嘛 那么单点维护 区间剩余卡片和最小值 每次知道最小值之后,怎么知道需要修改的位置呢 直接从每种数维护的set找到现在需要修改 ...

  9. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

随机推荐

  1. SharePoint ULS Log Viewer 日志查看器

    SharePoint ULS Log Viewer 日志查看器 项目描写叙述 这是一个Windows应用程序,更加轻松方便查看SharePoint ULS日志文件.支持筛选和简单的视图. 信息 这是一 ...

  2. jave 金额科学记数法处理

    金额 :amount amount.stripTrailingZeros().toPlainString();

  3. Rest Api(转载)

    来源:http://www.cnblogs.com/springyangwc/archive/2012/01/18/2325784.html 概述 REST 从资源的角度来观察整个网络,分布在各处的资 ...

  4. ubuntu搭建mysql

    步骤1 – 安装MySQL 在 Ubuntu 16.04 中,默认情况下,只有最新版本的 MySQL 包含在 APT 软件包存储库中.在撰写本文时,那是 MySQL 5.7 要安装它,只需更新服务器上 ...

  5. CoreData使用

    1.如果想创建一个带有coreData的程序,要在项目初始化的时候勾选中 2.创建完成之后,会发现在AppDelegate里多出了几个属性,和2个方法 <span style="fon ...

  6. Partition算法及Partition算法用于快速排序

    JavaScript简单方便,所以用JavaScript实现,可以在Chrome控制台下观察运行结果.主要实现Partition算法,比如输入为   var array = [4, 2, 1, 3, ...

  7. PHP上传文件时,文件过大$_FILES为空

    在php中判断上传文件的大小,但是文件一但过大,print_r($_FILES);的值就变为null了,有时候大家会遇到这么一个问题,上传小文件时,PHP能正常获取到,但是文件一超过8M就变为空了,我 ...

  8. [Tyvj Aug11] 黄金矿工

    传送门 Description 黄金矿工是一个经典的小游戏,它可以锻炼人的反应能力.该游戏中,可以通过“挖矿”获得积分并不断升级.玩家可以在线玩flash版黄金矿工,也可以下载后玩单机版黄金矿工.目前 ...

  9. tflearn 数据集太大无法加载进内存问题?——使用image_preloader 或者是 hdf5 dataset to deal with that issue

    tflearn 数据集太大无法加载进内存问题? Hi, all! I'm trying to train deep net on a big dataset that doesn't fit into ...

  10. VOIP语音编码带宽计算

    VOIP Bandwidth consumption naturally depends on the codec used.  VOIP消耗的带宽一般取决于所使用的语音编码. When calcul ...