UVA 1262 Password 暴力枚举

Password

Time Limit: 3000ms

Memory Limit: 131072KB

This problem will be judged on UVA.
Original ID: 1262

64-bit integer IO format: %lld     
Java class name: Main

Prev

Submit Status Statistics Discuss

Next

[PDF Link]

Shoulder-surfing is the behavior of intentionally and stealthily watching the screen of another person‘s electronic device, such as laptop computer or mobile phone. Since mobile devices prevail, it is getting serious to steal personal information by shoulder-surfing.

Suppose that we have a smart phone. If we touch the screen keyboard directly to enter the password, this is very vulnerable since a shoulder-surfer easily knows what we have typed. So it is desirable to conceal
the input information to discourage shoulder-surfers around us. Let me explain one way to do this.

You are given a 6 x 5 grid. Each column can be considered the visible part of a wheel. So you can easily rotate
each column wheel independently to make password characters visible. In this problem, we assume that each wheel contains the 26 upper letters of English alphabet. See the following Figure 1.

Figure 1. 6 x 5 window clips a valid grid representation for a password.

Assume that we have a length-5 password such as p1 p2 p3 p4 p5.
In order to pass the authentication procedure, we should construct a configuration of grid space where each piappears
in the i-th column of the grid. In that situation we say that the user password is accepted.

Let me start with one example. Suppose that our password was set `COMPU‘. If we construct the grid as shown in Figure 2 on next page, then the authentication is successfully processed.

Figure 2. A valid grid representation for password `COMPU‘.

In this password system, the position of each password character in each column is meaningless. If each of the 5 characters in p1 p2 p3 p4 p5 appears
in the corresponding column, that can be considered the correct password. So there are many grid configurations allowing one password. Note that the sequence of letters on each wheel is randomly determined for each trial and for each column. In practice, the
user is able to rotate each column and press ``Enter" key, so a should-surfer cannot perceive the password by observing the 6 x 5grid since there are too
many password candidates. In this 6 x 5 grid space, maximally 65 =
7, 776 cases are possible. This is the basic idea of the proposed password system against shoulder-surfers.

Unfortunately there is a problem. If a shoulder-surfer can observe more than two grid plate configurations for a person, then the shoulder-surfer can reduce the searching space and guess the correct password. Even
though it is not easy to stealthily observe other‘s more than once, this is one weakness of implicit grid passwords.

Let me show one example with two observed configurations for a grid password. The user password is `COMPU‘, but `DPMAG‘ is also one candidate password derived from the following configuration.

Figure 3. Both of `COMPU‘ and `DPMAG‘ are feasible password .

You are given two configurations of grid password from a shoulder-surfer. Suppose that you have succeeded to stealthily record snapshots of the target person‘s device (e.g. smart phone). Then your next task is to
reconstruct all possible passwords from these two snapshots. Since there are lots of password candidates, you are asked for the k-th password among
all candidates in lexicographical order. In Figure 3, let us show the first 5 valid password. The first 5 valid passwords are `ABGAG‘ , `ABGAS‘, `ABGAU‘, `ABGPG‘ and `ABGPS‘.

The number k is given in each test case differently. If there does not exist a k-th
password since k is larger than the number of all possible passwords, then you should print `NO‘ in the output.

Input

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is
given in the first line of the input. The first line of each test case contains one integer, K, the order of the password you should find. Note that 1K7,
777. Next the following 6 lines show the 6 rows of the first grid and another 6 lines represent the 6 rows of the second grid.

Output

Your program is to write to standard output. Print exactly the k-th password (including `NO‘) in one line for each test case.

The following shows sample input and output for three test cases.

Sample Input

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

Sample Output

ABGAG
ABGPS
NO

Prev

Submit Status Statistics Discuss

Next


Distributed under GPLv3. | Project Homepage | Developer: 51isoft |
Current Style: Cerulean.

/* ***********************************************
Author        :CKboss
Created Time  :2014年12月15日 星期一 10时08分57秒
File Name     :UVA1262.cpp
 ************************************************ */

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

using namespace std;

int k;
char m1[10][10],m2[10][10];
vector<char> vc[10];
vector<string> vs;
char str[10];

void dfs(int deep)
{
	if(deep==5)
	{
		string ans;
		for(int i=0;i<5;i++)
			ans+=str[i];
		vs.push_back(ans);
		return ;
	}
	for(int i=0,sz=vc[deep].size();i<sz;i++)
	{
		str[deep]=vc[deep][i];
		dfs(deep+1);
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d",&k);
		for(int i=0;i<6;i++)
			scanf("%s",m1[i]);
		for(int i=0;i<6;i++)
			scanf("%s",m2[i]);

		for(int j=0;j<5;j++)
		{
			vc[j].clear();
			for(int i=0;i<6;i++)
			{
				char c1=m1[i][j];
				bool flag=false;
				for(int i2=0;i2<6&&flag==false;i2++)
				{
					if(m2[i2][j]==c1) flag=true;
				}
				if(flag==true)
				{
					vc[j].push_back(c1);
				}
			}
		}

		vs.clear();
		dfs(0);
		sort(vs.begin(),vs.end());
		int tt=unique(vs.begin(),vs.end())-vs.begin();
		if(k>tt) puts("NO");
		else
		{
			cout<<vs[k-1]<<endl;
		}
	}

	return 0;
}
时间: 2024-08-06 20:08:29

UVA 1262 Password 暴力枚举的相关文章

【暑假】[数学]UVa 1262 Password

UVa 1262  Password 题目: Password Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Shoulder-surfing is the behavior of intentionally and stealthily watching the screen of another person's electronic de

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

题意:给两个6行5列的字母矩阵,找出满足如下条件的"密码":密码中的每个字母在两个矩阵的对应列中均出现.给定k(1<=k<=7777),你的任务是找出字典序第k小的密码.如果不存在,输出NO. 分析:因为k<=7777,直接按字典序从小到大的顺序递归一个一个的枚举. 注意:定义在dfs里的vis不能放在全局,否则会导致值的混用. #pragma comment(linker, "/STACK:102400000, 102400000") #incl

uva 1508 Equipment(暴力+枚举子集)

uva 1508 Equipment 题目大意:给出n个5元组,要求从中选取k个,要求5个位置上的数的最大值的和尽量大. 解题思路:一开始没思路,看别人题解过的.5位可以组成32种情况,在DFS前预处理,找出32种情况在n组数据中的最大值,然后将这组数据带入DFS中枚举计算. #include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> using namespace

UVa1262 - Password(暴力枚举)

题意: 给出两个6行5列的字母矩阵,一个密码满足:密码的第i个字母在两个字母矩阵的第i列均出现. 然后找出字典序为k的密码,如果不存在输出NO 分析: 我们先统计分别在每一列均在两个矩阵出现的字母,然后从小到大排好序. 对于第一个样例来说,我们得到ACDW.BOP.GMOX.AP.GSU 则一共有4×3×4×2×3=288种密码,我们先计算这个数列的后缀积:288.72.24.6.3.1 要确定第一个字母,如果1≤k≤72,则是A:如果73≤k≤144,则是C,以此类推. 确定第二个字母是类似的

UVA 1262 Password

https://vjudge.net/problem/UVA-1262 字典序第k小 注意两点: 1. k-- 2.去重 #include<cstring> #include<cstdio> #include<set> #include<algorithm> using namespace std; char s[3][7][6]; int sum[6][6],ans[6],suc[7]; set<char>se[7]; int main() {

uva 725 DIVISION (暴力枚举)

我的56MS 1 #include <cstdio> 2 #include <iostream> 3 #include <string> 4 #include <cstring> 5 #include <queue> 6 #include <vector> 7 #include <map> 8 #include <cmath> 9 using namespace std; 10 11 #define MEM(a

uva 725 Division(暴力枚举) 解题心得

原题: Description Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9once each, such that the first number divided by the second is equal to an integer N, where . That is, abcde / fghij =N w

UVa 1262 - Password(组合数)

给出两个6*5矩阵,有一个5位的密码,密码的第i位必须在两个矩阵的第i列都出现过,问输出字典序第k大的满足条件的密码,无解输出"NO". 预处理出每一位满足条件的字母,然后计算后几位密码可行的种数.对k进行判断后输出,具体细节见代码. #include<cstdio> #include<cstring> #include<vector> using namespace std; char s[2][7][6]; bool has[2][6][27];

uva 565 - Pizza Anyone?(暴力枚举 + 二进制)

题目:uva 565 - Pizza Anyone?(暴力枚举 + 二进制) 题目大意:题目是说有一个人要帮他的朋友们定批萨,然后每个朋友都有自己的口味要求,问能不能定一个批萨然后满足每个朋友的至少一个要求. 能就输出所定批萨里面加的东西,,输出要求按字典序: 不能就输出:No pizza can satisfy these requests. 解题思路:这题里面有16种材料,每种材料只有取与不取的可能,这样就有 2^16 种( 0 - 2^16 - 1),枚举出每种情况然后在分别看是否能满足每