Test instructions
Give two 6 rows and 5 columns of the letter matrix, a password satisfies: The first letter of the password in the two letter matrix in column I appear.
Then find the password for the dictionary order K, if there is no output
Analysis:
We first count the letters that appear in each column in each of the two matrices, and then order from small to large.
For the first example, we get ACDW, BOP, Gmox, AP, GSU
There is a total of 4x3x4x2x3=288 code, we first calculate the suffix product of this series: 288, 72, 24, 6, 3, 1
To determine the first letter, if 1≤k≤72, it is a; if 73≤k≤144, then C, and so on.
Make sure the second letter is similar, using k%72+1 to compare with 24.
In the code implementation, the dictionary order starts at 0.
Solution Two:
Since passwords are upto 6 5 = 7776, they can be enumerated from small to large in dictionary order.
The difficulty of thinking, writing faster and more right.
#include <cstdio> #include <cstring>using namespace Std;int k,cnt;char g[2][6][5],ans[6];bool dfs (int col) { if (col==5) { if (++cnt==k) { ans[col]= ' + '; printf ("%s\n", ans); return true; } return false; } BOOL Vis[2][26]; memset (vis,false,sizeof (Vis)); for (int i=0;i<2;i++) for (int j=0;j<6;j++) vis[i][g[i][j][col]-' A ']=1; for (int i=0;i<26;i++) if (Vis[0][i]&&vis[1][i]) { ans[col]=i+ ' A '; if (Dfs (col+1)) return true; } return false;} int main () { int T; scanf ("%d", &t); while (t--) { scanf ("%d", &k); for (int i=0;i<2;i++) for (int j=0;j<6;j++) scanf ("%s", G[i][j]); cnt=0; if (!dfs (0)) puts ("NO"); } return 0;}
Uva1262-password (brute Force enumeration)