Uva1262-password (brute Force enumeration)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.