uva1262

 1                               
 2                           /*  解码 _________________________________________________________________________________
 3                             
 4                               #include <iostream> 
 5                               #include <map> 
 6                               #include <cmath>
 7                               #include <vector>
 8                               #include <cstdio>
 9                               #include <string>
10                               #include <cstring> 
11                               #include <algorithm>    
12                               using namespace std; 
13                               #define fir first
14                               #define sec second
15                               #define pb(x) push_back(x) 
16                               #define mem(A, X) memset(A, X, sizeof A)
17                               #define REP(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
18                               #define rep(i,l,u) for(int (i)=(int)(l);(i)>=(int)(u);--(i)) 
19                               #define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e) 
20                               typedef long long LL;   
21                               typedef unsigned long long ull;
22                               typedef pair<long,long>  pll;     
23                               
24                               
25                               LL T,n;
26                               int k;
27                               const int mod=1e9+7; 
28                               const int maxn=1e5+10; 
29                               char s[2][6][10],ans[10];
30                               int cnt;
31                               bool dfs(int col)
32                               {
33                                   if(col==5)
34                                   {
35                                       if(++cnt==k) 
36                                       { 
37                                           ans[5]='\0';
38                                           printf("%s\n",ans); 
39                                           return true;
40                                       }
41                                       else 
42                                           return false;
43                                   }
44                                   else
45                                   {
46                                       bool vis[2][26];
47                                       mem(vis,false);
48 
49                                       REP(i,0,1)
50                                        REP(j,0,5) 
51                                             vis[i][ s[i][j][col]-'A' ]=true; //只处理当前列对应的可能位置,搜索时只处理当前层。
52  
53                                        REP(j,0,25)  
54                                             if(vis[0][j]==true && vis[1][j]==true)
55                                                 {
56                                                     ans[col]=j+'A';
57                                                     if( dfs(col+1) ) return true;
58                                                 }  
59                                   }
60                                   return false;
61 
62                               }
63 
64                               int main()
65                               {
66                                    freopen("in.txt","r",stdin); 
67                                    //while(cin>>n)
68                                    while(scanf("%d",&T)!=EOF)
69                                    { 
70                                      REP(kase,1,T)  
71                                      {
72                                          scanf("%d",&k);
73                                          REP(i,0,1)
74                                           REP(j,0,5) 
75                                               {
76                                                   scanf("%s",&s[i][j]);
77                                                   //printf("%s\n",s[i][j]);
78                                               }
79                                          cnt=0;
80                                          if(!dfs(0)) puts("NO");  
81                                       }
82                   
83                                    }
84                                 return 0;
85                               }
86                   
87                              /*
88                                 note    :   编码理论  
89                                                 本题运用的暴力方法,编写简单,
90                                                 如果用直接构造性的编码,实现时要注意更多的细节。
91 
92 
93                                 debug   :   
94                                 optimize:
95                                             直接操纵输入的字符,减少中间的传递,简化过程。
96                                             二维的数组字母表,处理多个相似的对象时进行优化。
97                               */ 

 

posted @ 2016-11-17 20:39  TechIsOnlyTool  阅读(200)  评论(0编辑  收藏  举报