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. Juce源代码分析(九)应用程序基类ApplicationBase

    在前面的几篇文章,分析的都是Juce库里面Core模块的内存部分,除了骨灰级C++爱好者之外,貌似大家对这些都不是非常感兴趣.相信大家更想知道Juce是怎么用于产品开发,而对于它的构成不是非常感兴趣. ...

  2. Clustering of residential areas based on residential conditions

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ3hpYW5neXVpYm0=/font/5a6L5L2T/fontsize/400/fill/I0 ...

  3. WinCE下使用C#的几个小技巧

    1.我们知道,在使用Windows的开发机上用C#启动一个外部程序的方法有很多,但这些方法用在使用WinCE的目标工控机上都无能为力,现在小嫚儿以打开一个IE为例,介绍如何在WinCE下使用C#来打开 ...

  4. Object/Relational Mapping 数学关系 反面向对象

    [hibernate ORM 是对象关系映射框架 事实上的持久化存储引擎] http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/ ...

  5. the algebra of modulo-2 sums disk failure recovery

    x=y x_+_y=0 The bit in any position is the modulo-2 sum of all the bits in the corresponding positio ...

  6. 点击文本选中checkbox

    <checbox文本编辑/>      : 只点击checkbox时,才可以选中,点击文本时无法选中 <label><checbox文本编辑/></label ...

  7. Block浅析一

    1.在Block结构体中含有isa指针,这就证明了Block其实就是对象,并具有一般对象的所有功能. 2.Block是OC中的一种数据类型,在iOS开发中被广泛使用. 3.block的应用 (1)遍历 ...

  8. Oracle Meger into 函数

    Oracle 在 9i 引入了 merge 命令, 通过这个 merge 能够在一个SQL 语句中对一个表同时执行 inserts 和 updates 操作.Merge into 可以实现用 B 表来 ...

  9. linux常用命令与技巧(不断添加与更新)

    linux常用命令:linux命令有很多,不去做全面的介绍与总结,这里只是记录在使用linux过程中遇到的常用命令和技巧,以便以后查阅. # mkdir -p /usr/local/src/ ##递归 ...

  10. h5 移动端 关于监测切换程序到后台或息屏事件和visibilitychange的使用

    需求:当我们页面上正在播放视频或者播放背景音乐时,我们屏幕自动息屏或者切换程序去看消息时,我们希望暂停视频或背景音乐,回到程序我们希望继续播放视频或播放背景音乐.小程序上提供了 onUnload返回 ...