码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 1262 Password(密码)(暴力枚举)

时间:2017-02-11 00:47:33      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:unsigned   密码   type   全局   print   scan   uva   递归   [1]   

题意:给两个6行5列的字母矩阵,找出满足如下条件的“密码”:密码中的每个字母在两个矩阵的对应列中均出现。给定k(1<=k<=7777),你的任务是找出字典序第k小的密码。如果不存在,输出NO。

分析:因为k<=7777,直接按字典序从小到大的顺序递归一个一个的枚举。

注意:定义在dfs里的vis不能放在全局,否则会导致值的混用。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 30000000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char pic[2][6][5];
char ans[6];
int k;
int cnt;
bool dfs(int cur){
    if(cur == 5){
        if(++cnt == k){
            ans[cur] = ‘\0‘;
            return true;
        }
        return false;
    }
    int vis[2][26];
    memset(vis, 0, sizeof vis);
    for(int i = 0; i < 2; ++i){
        for(int j = 0; j < 6; ++j){
            vis[i][pic[i][j][cur] - ‘A‘] = 1;
        }
    }
    for(int i = 0; i < 26; ++i){
        if(vis[0][i] && vis[1][i]){
            ans[cur] = ‘A‘ + i;
            if(dfs(cur + 1)) return true;
        }
    }
    return false;
}
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        cnt = 0;
        scanf("%d", &k);
        for(int i = 0; i < 2; ++i){
            for(int j = 0; j < 6; ++j){
                scanf("%s", pic[i][j]);
            }
        }
        if(!dfs(0)){
            printf("NO\n");
        }
        else{
            printf("%s\n", ans);
        }
    }
    return 0;
}

  

UVA - 1262 Password(密码)(暴力枚举)

标签:unsigned   密码   type   全局   print   scan   uva   递归   [1]   

原文地址:http://www.cnblogs.com/tyty-Somnuspoppy/p/6388398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!