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

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

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

转载于:https://www.cnblogs.com/Yinku/p/10280299.html

Codeforces - 65D - Harry Potter and the Sorting Hat - 简单搜索相关推荐

  1. Codeforces Round #699 (Div. 2) E.Sorting Books(贪心+DP / 线段树)超高质量题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 E - Sorting Books 一排书架上有 nnn 本书排成一排,每本书上有一个颜色 aia_i ...

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

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

  3. 【CodeForces - 205B 】Little Elephant and Sorting (思维)

    题干: The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the ...

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

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

  5. Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs

    题目链接: http://codeforces.com/contest/525/problem/D 题意 给你一个n*m的田地,有一些*的地方是可以移除变成"."的,然后问你移除最 ...

  6. CODEFORCES 272C Dima and Staircase 细节理解题+简单技巧

    传送门:http://codeforces.com/problemset/problem/272/C C. Dima and Staircase time limit per test 2 secon ...

  7. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

  8. Codeforces Round #540 (Div. 3)--A. Water Buying(简单思维题-有点坑)

    A. Water Buying 题目链接http://codeforces.com/problemset/problem/1118/A time limit per test:1 second mem ...

  9. codeforces 贪心+优先队列_算法基础04-深度优先搜索、广度优先搜索、二分查找、贪心算法...

    深度优先搜索DFS.广度优先搜索BFS 比较 拿谚语打比方的话,深度优先搜索可以比作打破砂锅问到底.不撞南墙不回头:广度优先搜索则对应广撒网,多敛鱼 两者没有绝对的优劣之分,只是适用场景不同 当解决方 ...

最新文章

  1. 如何使用Github管理自己的代码
  2. oracle怎么查别的库,ORACLE_SID、实例名和数据库名的区别及查看方法
  3. 关于一些朋友想做在线教育的回复和分享
  4. 求最大素数的c语言,for语句计算输出10000以内最大素数怎么搞最简单??各位大神们...
  5. NOIP模拟测试6「那一天我们许下约定(背包dp)·那一天她离我而去」
  6. Android 隐藏状态栏,沉浸式状态栏,状态栏背景色,状态栏字体色,透明状态工具类
  7. fox pro删除单条数据_Mac文件夹数据同步工具——Sync Folders Pro
  8. Ubuntu 20.04 安装 CUDA10.0和cudnn7.6
  9. matlab7.0.4 license,MATLAB 7.0安装??License Manager Error-9~唉
  10. 审计计算机考试报名,审计业务考试计算机(5页)-原创力文档
  11. 使用Fiddler自定义百度云分享提取码
  12. 用Vue.js递归组件构建一个可折叠的树形菜单
  13. Algorithm -- 字母重排
  14. spring-第十一篇之SpEL表达式
  15. 大三计算机专业下在哪投实习简历,大三实习生简历通用模板
  16. IAR MCS-51 v7.51A 软件注册机下载
  17. Fatal error: init(coder:) has not been implemented in Swfit
  18. 中国IT公司百强排名
  19. 锡兰1.1.0现已上市
  20. 直通车优化,淘宝运营如何优化直通车

热门文章

  1. poj3264Balanced Lineup(倍增ST表)
  2. 我记录网站综合系统 -- 技术原理解析[0:简介(代序) 1.7Beta源代码下载开始]...
  3. RoRoWoBlog 开源博客系统介绍
  4. Vue和iview-admin搭建的项目进行兼容
  5. [Java核心技术(卷I)] - 简易的日历
  6. 1 State Hook
  7. initialize方法与load方法比较
  8. .NET Core 3.0中的数据库驱动框架System.Data
  9. 微软桌面虚拟化vdi 安装测试体验
  10. byte转化为Bitmap,防止内存溢出