Password UVA - 1262

題目傳送門

題意;給你兩個6行5列的密碼矩陣,找出滿足下列條件的“密碼”:密碼中的每一個字母都在兩個矩陣的對應列中出現,求字典序第k大的的密碼,如果沒有輸出NO。

題意:直接暴力瞎搞一下就好了。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MAXN 10
#define MAXE 400
#define INF 100000000
#define MOD 10001
#define LL long long
#define pi 3.14159

using namespace std;

string matrix1[MAXN];
string matrix2[MAXN];
set<char> Set[MAXN];

int main() {
    std::ios::sync_with_stdio(false);
    int T;
    cin >> T;
    for (int kase = 1; kase <= T; ++kase) {
        int k;
        for (int i = 0; i < MAXN; ++i) {
            Set[i].clear();
        }
        cin >> k;
        for (int i = 0; i < 6; ++i) {
            cin >> matrix1[i];
        }
        for (int i = 0; i < 6; ++i) {
            cin >> matrix2[i];
        }
        set<char> s;
        for (int i = 0; i < 5; ++i) {
            s.clear();
            for (int j = 0; j < 6; ++j) {
                s.insert(matrix1[j][i]);
            }
            for (int j = 0; j < 6; ++j) {
                if (s.count(matrix2[j][i])) {
                    Set[i].insert(matrix2[j][i]);
                }
            }
        }
        if (Set[0].size() * Set[1].size() * Set[2].size() * Set[3].size() * Set[4].size() < k) {
            cout << "NO\n";
        } else {
            int num1 = (int) Set[1].size();
            int num2 = (int) Set[2].size();
            int num3 = (int) Set[3].size();
            int num4 = (int) Set[4].size();
            vector<int> vec;
            int pos = k / (num1 * num2 * num3 * num4);
            k %= (num1 * num2 * num3 * num4);
            if (k == 0) {
                vec.push_back(pos - 1);
                k = num1 * num2 * num3 * num4;
            } else {
                vec.push_back(pos);
            }
            pos = k / (num2 * num3 * num4);
            k %= (num2 * num3 * num4);
            if (k == 0) {
                vec.push_back(pos - 1);
                k = num2 * num3 * num4;
            } else {
                vec.push_back(pos);
            }
            pos = k / (num3 * num4);
            k %= (num3 * num4);
            if (k == 0) {
                vec.push_back(pos - 1);
                k = num3 * num4;
            } else {
                vec.push_back(pos);
            }
            pos = k / num4;
            k %= num4;
            if (k == 0) {
                vec.push_back(pos - 1);
                k = num4;
            } else {
                vec.push_back(pos);
            }
            pos = k;
            vec.push_back(pos - 1);
            string str = "\0";
            for (int i = 0; i < 5; ++i) {
                int ans = 0;
                for (set<char>::iterator it = Set[i].begin(); it != Set[i].end(); ++it) {
                    if (ans == vec[i]) {
                        str += *it;
                        break;
                    }
                    ans++;
                }
            }
            cout << str << endl;
        }
    }
    return 0;
}

/*
 3
 1
 AYGSU
 DOMRA
 CPFAS
 XBODG
 WDYPK
 PRXWO
 CBOPT
 DOSBG
 GTRAR
 APMMS
 WSXNU
 EFGHI
 5
 AYGSU
 DOMRA
 CPFAS
 XBODG
 WDYPK
 PRXWO
 CBOPT
 DOSBG
 GTRAR
 APMMS
 WSXNU
 EFGHI
 64
 FGHIJ
 EFGHI
 DEFGH
 CDEFG
 BCDEF
 ABCDE
 WBXDY
 UWYXZ
 XXZFG
 YYFYH
 EZWZI
 ZGHIJ
 */
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章