Problem 1074: Hey Judge

Time Limits:  1000 MS   Memory Limits:  65536 KB

64-bit interger IO format:  %lld   Java class name:  Main

 

Description

Judge Nicole collected 7 ideas for problems of different levels, she wants to create 5 problems for the next contest, one for each difficulty level, from A to E (difficulty 1 to 5). Given the difficulty level of the problems she currently has, she can merge the ideas of two problems, one of level x, and the other of level y to get a problem of level x + y.

For example, Judge Nicole can merge two problems of difficulties A and D, to get one problem of difficulty E (1 + 4 = 5).

Merging more than two problems into one will produce a problem with a long statement which is hard to explain, so she won’t do this (i.e., each problem is merged with another at most once). Also, she can’t merge a resultant problem again, and she can't use the same problem twice.

On the next contest, Output YES if the collected questions include A to E, otherwise the output NO.

Input

The first line of input contains an integer T (1 ≤ T ≤ 300), the number of test cases.

Each test case will contain only one string S of length 7. Each letter of the string represents the difficulty level of a problem (from A to E), 'A' is the easiest and 'E' is the hardest.

Output

For each test case print "YES" if she can prepare a contest using the current problems, otherwise print "NO".

Sample Input

  1. 3
  2. EBEABDA
  3. CEDEACA
  4. BDAAEAA

Output for Sample Input

  1. YES
  2. NO
  3. YES

周赛的题目,裸字典树那题ZZ叶子忘记判断一直WA,这题又理解成贪心瞎搞搞调试半天WA,DP那题又不会,最后可惜地爆0结尾了,还是too young too naive……DFS回溯暴搜的题目,下午突然有灵感发现题目并不难。

由于情况太多不知道到底当前的字母是由另外两个合成的还是用原来就有的,感觉不是贪心,那就直接用DFS分情况搜索,假设某个难度的字母就是用原来的、或者是其他两个合成的,两种情况回溯地搜一下就好

代码:

  1. #include <stdio.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define INF 0x3f3f3f3f
  5. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  6. #define LC(x) (x<<1)
  7. #define RC(x) ((x<<1)+1)
  8. #define MID(x,y) ((x+y)>>1)
  9. typedef pair<int,int> pii;
  10. typedef long long LL;
  11. const double PI=acos(-1.0);
  12. const int N=15;
  13. char s[N];
  14. int vis[N],ok[N];
  15. bool flag;
  16. inline int getid(const char &x)
  17. {
  18. return x-'A'+1;
  19. }
  20. void dfs(int now)
  21. {
  22. int tempflag=1;
  23. for (int i=1; i<=5; ++i)
  24. {
  25. if(!ok[i])
  26. {
  27. tempflag=0;
  28. break;
  29. }
  30. }
  31. if(tempflag)
  32. flag=true;
  33. if(flag||now>=6)
  34. return ;
  35. for (int i=0; i<7; ++i)
  36. {
  37. if(vis[i])
  38. continue;
  39. if(getid(s[i])==now)
  40. {
  41. vis[i]=1;
  42. ok[now]=1;
  43. dfs(now+1);
  44. ok[now]=0;
  45. vis[i]=0;
  46. }
  47. for (int j=0; j<7; ++j)
  48. {
  49. if(vis[j]||i==j)
  50. continue;
  51. int sum=getid(s[i])+getid(s[j]);
  52. if(sum==now)
  53. {
  54. vis[i]=1;
  55. vis[j]=1;
  56. ok[now]=1;
  57. dfs(now+1);
  58. vis[i]=0;
  59. vis[j]=0;
  60. ok[now]=0;
  61. }
  62. }
  63. }
  64. }
  65. int main(void)
  66. {
  67. int n;
  68. scanf("%d",&n);
  69. while (n--)
  70. {
  71. fill(s,s+N,'\0');
  72. fill(vis,vis+N,0);
  73. fill(ok,ok+N,0);
  74. scanf("%s",s);
  75. flag=false;
  76. dfs(1);
  77. puts(flag?"YES":"NO");
  78. }
  79. return 0;
  80. }

NOJ 1074 Hey Judge(DFS回溯)的更多相关文章

  1. 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯

      算法提高 8皇后·改   时间限制:1.0s   内存限制:256.0MB      问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...

  2. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  3. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  7. P1074 靶形数独 dfs回溯法

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...

  8. 剪格子---(dfs回溯)

    如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...

  9. poj1270Following Orders(拓扑排序+dfs回溯)

    题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...

随机推荐

  1. 【转】win8.1下安装ubuntu

    参考:http://jingyan.baidu.com/article/14bd256e0ca52ebb6d26129c.html Ubuntu 系统是一款优秀的.基于GNU/Linux 的平台的桌面 ...

  2. Python基础2- Hello,world

    第一个程序Hello,world! 交互式编程:在终端窗口中输入:python回车后直接进入python交互模式,然后在提示符>>>后面输入:print 'Hello,world!' ...

  3. BZOJ3591: 最长上升子序列

    因为是一个排列,所以可以用$n$位二进制数来表示$O(n\log n)$求LIS时的单调栈. 首先通过$O(n^22^n)$的预处理,求出每种LIS状态后面新加一个数之后的状态. 设$f[i][j]$ ...

  4. echarts.js 做图表的插件

    <scripttype="text/javascript"src="{uiurl()}echarts/echarts.js"></script ...

  5. http 上传文件

    @RequestMapping(method=RequestMethod.POST,value = "/upload") public ModelAndView processIm ...

  6. 【BZOJ2473/2120】维护队列 分块+二分

    Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会 ...

  7. 开发android过程中eclipse闪退解决

    有一次eclipse崩溃了,然后再双击就无法打开了.换了几个版本也是如此. 后来找到了这个方法:删除文件 [workspace]/.metadata/.plugins/org.eclipse.e4.w ...

  8. bug:clang: error: no input files

    1.clang: error: no input files这个问题一般是因为你删除或者移动了某一个文件,但是在你的编译资源里面( project > target > Build Pha ...

  9. The beatles-Yesterday

    轉載自https://www.youtube.com/watch?v=XNnaxGFO18o Yesterday Lyrics:Paul Mccartney Music:Paul Mccartney ...

  10. MAT(Memory Analyzer Tool)工具入门介绍

    1.MAT是什么? MAT(Memory Analyzer Tool),一个基于Eclipse的内存分析工具,是一个快速.功能丰富的JAVA heap分析工具,它可以帮助我们查找内存泄漏和减少内存消耗 ...