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. python 修饰符(转载)

    首先一个修饰符的实例: #!/usr/bin/env python def run(fn): def do_hello(): print "begin..." fn() print ...

  2. 修改live555支持mpeg2ts RTSP拉流,附代码

    在很早之前的博客<用EasyDarwin进行IPTV rtsp mpeg-ts smil流的转发和分发直播服务>中,我们介绍到如何将live555支持mpeg2ts拉流,这个在IPTV领域 ...

  3. Package template (html/template) ... Types HTML, JS, URL, and others from content.go can carry safe content that is exempted from escaping. ... (*Template) Funcs ..

    https://godoc.org/text/template GoDoc Home About Go: text/templateIndex | Examples | Files | Directo ...

  4. target!

    工作到现在也有8个月了,从学生时代想从事嵌入是开发,到工作中从事android开发,跨度还是比较大的:曾经想从事这些消费类电子产品的开发,想从一个用户变成一个生产者,但是真正进入到这一行之后,才知道在 ...

  5. h5的缓存机制

    H5的缓存,大概有localstorage.sessionstorage.cookie和manifest. 一.LocalStorage LocalStorage是永久性的本地缓存,存储在客户端的浏览 ...

  6. 创建一个catkin工作空间

    先确定自己的环境变量是否设置正确 export | grep ROS 若出现如下的,说明是正确的 declare -x ROSLISP_PACKAGE_DIRECTORIES="" ...

  7. # <center>merge表不可用的问题</center>

    最近碰到了个很有意思的问题,值得一写 给merge表和基础表添加索引时发生了一个问题,不管是先给merge表加索引还是基础表加索引,如果表数据量大都会导致加索引期间对merge表的查询不可用,因为使用 ...

  8. ModuleNotFoundError: No module named 'numpy.core._multiarray_umath' ImportError: numpy.core.multiarray failed to import

      出现以下错误:可能是因为你的numpy版本太低 更新numpy的版本 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgra ...

  9. 通过Chrome浏览器进行android调试/Remote Debugging on Android with Chrome

    The way your web content behaves on mobile can be dramatically different from the desktop experience ...

  10. 关于 josnp ,解决跨域问题

    JSONP是服务器与客户端跨源通信的常用方法.最大特点就是简单适用,老式浏览器全部支持,服务器不用做任何改造[使用jsonp的时候jsonp: "callback",callbac ...