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
3 EBEABDA CEDEACA BDAAEAA
Output for Sample Input
YES NO YES
周赛的题目,裸字典树那题ZZ叶子忘记判断一直WA,这题又理解成贪心瞎搞搞调试半天WA,DP那题又不会,最后可惜地爆0结尾了,还是too young too naive……DFS回溯暴搜的题目,下午突然有灵感发现题目并不难。
由于情况太多不知道到底当前的字母是由另外两个合成的还是用原来就有的,感觉不是贪心,那就直接用DFS分情况搜索,假设某个难度的字母就是用原来的、或者是其他两个合成的,两种情况回溯地搜一下就好
代码:
#include <stdio.h> #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) typedef pair<int,int> pii; typedef long long LL; const double PI=acos(-1.0); const int N=15; char s[N]; int vis[N],ok[N]; bool flag; inline int getid(const char &x) { return x-‘A‘+1; } void dfs(int now) { int tempflag=1; for (int i=1; i<=5; ++i) { if(!ok[i]) { tempflag=0; break; } } if(tempflag) flag=true; if(flag||now>=6) return ; for (int i=0; i<7; ++i) { if(vis[i]) continue; if(getid(s[i])==now) { vis[i]=1; ok[now]=1; dfs(now+1); ok[now]=0; vis[i]=0; } for (int j=0; j<7; ++j) { if(vis[j]||i==j) continue; int sum=getid(s[i])+getid(s[j]); if(sum==now) { vis[i]=1; vis[j]=1; ok[now]=1; dfs(now+1); vis[i]=0; vis[j]=0; ok[now]=0; } } } } int main(void) { int n; scanf("%d",&n); while (n--) { fill(s,s+N,‘\0‘); fill(vis,vis+N,0); fill(ok,ok+N,0); scanf("%s",s); flag=false; dfs(1); puts(flag?"YES":"NO"); } return 0; }