UVa1262 - Password

不知道哪错了。。0.0

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
const int INF = ~0U >> 1;
const int maxn = 7;
char s1[maxn][maxn], s2[maxn][maxn];
string str[maxn];
int T, k, cnt = 0;

void init() {
    memset(s1, 0, sizeof(s1));
    memset(s2, 0, sizeof(s2));
    for(int i = 0; i < maxn; ++i) str[i].clear();
    return ;
}

int dfs(int n, char* ans) {
    if(n == 5) {
        ++cnt;
        if(cnt == k) {
            printf("%s\n", ans);
            return 1;
        }
        return 0;
    }
    for(int i = 0; i < str[n].size(); ++i) {
        ans[n] = str[n][i];
        if(dfs(n + 1, ans)) return 1;
        ans[n] = 0;
    }
}

int main() {
    scanf("%d", &T);
    while(T--) {
        cnt = 0;
        init();
        scanf("%d", &k);
        for(int i = 0; i < 6; ++i)
            scanf("%s", s1[i]);
        for(int i = 0; i < 6; ++i)
            scanf("%s", s2[i]);

        for(int i = 0; i < 5; ++i) {
            int cnt = 0;
            for(int j = 0; j < 6; ++j)
                for(int k = 0; k < 6; ++k)
                    if(s1[j][i] == s2[k][i]) str[i].push_back(s1[j][i]);
        }
//        for(int i = 0; i < 5; ++i) cout << str[i] << endl;
        bool ok = true;
        for(int i = 0; i < 5; ++i) {
            unique(str[i].begin(), str[i].end());
            sort(str[i].begin(), str[i].end());
            if(str[i].size() == 0) ok = false;
        }
        if(!ok) {
            printf("NO\n");
            continue;
        }
        char s[maxn] = {0};
        if(!dfs(0, s)) printf("NO\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值