UVa 1262 - Password(解码)
链接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3703
题意:
给两个6行5列的字母矩阵,找出满足如下条件的“密码”:密码中的每个字母在两个矩阵的对应列中均出现。
例如,左数第2个字母必须在两个矩阵中的左数第2列中均出现。
给定k(1≤k≤7777),你的任务是找出字典序第k小的密码。如果不存在,输出NO。
分析:
经典的解码问题。首先求出答案每一列的所有可能字母,然后逐位解码即可。
具体实现见代码。
代码:
import java.io.*;
import java.util.*; public class Main {
static char G[][][] = new char[2][6][9]; public static void main(String args[]) {
Scanner cin = new Scanner(new BufferedInputStream(System.in)); int T = cin.nextInt();
while(T --> 0) {
int k = cin.nextInt();
cin.nextLine(); // 清空缓存区
for(int t = 0; t < 2; t++) {
for(int r = 0; r < 6; r++) {
G[t][r] = cin.nextLine().toCharArray();
}
} @SuppressWarnings("unchecked")
ArrayList<Character> a[] = new ArrayList[5]; // a[c]: 答案第c列的所有可能字母
for(int i = 0; i < 5; i++) a[i] = new ArrayList<Character>(); boolean has[][][] = new boolean[2][5][26]; // 第t个矩阵的第c列是否有字母ch
for(int c = 0; c < 5; c++) {
for(int t = 0; t < 2; t++) {
for(int r = 0; r < 6; r++) {
int ch = G[t][r][c] - 'A';
has[t][c][ch] = true;
}
}
for(int i = 0; i < 26; i++)
if(has[0][c][i] && has[1][c][i]) a[c].add((char)(i+'A'));
} int up = 1;
for(int i = 0; i < 5; i++) up *= a[i].size();
if(k > up) {
System.out.println("NO");
continue;
} k--; // 解码
for(int i = 0; i < 5; i++) {
up /= a[i].size();
System.out.print(a[i].get(k/up));
k %= up;
}
System.out.println();
}
cin.close();
}
}
UVa 1262 - Password(解码)的更多相关文章
- 【暑假】[数学]UVa 1262 Password
UVa 1262 Password 题目: Password Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- UVA 1262 Password 暴力枚举
Password Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: ...
- UVA 1262 Password
https://vjudge.net/problem/UVA-1262 字典序第k小 注意两点: 1. k-- 2.去重 #include<cstring> #include<cst ...
- UVA - 1262 Password(密码)(暴力枚举)
题意:给两个6行5列的字母矩阵,找出满足如下条件的“密码”:密码中的每个字母在两个矩阵的对应列中均出现.给定k(1<=k<=7777),你的任务是找出字典序第k小的密码.如果不存在,输出N ...
- UVA - 1262 数学
UVA - 1262 题意: 有两个6*5 的大写字母组成的矩阵,需要找出满足条件的字典序第k小的密码:密码中每个字母在两个矩阵的对应的同一列中都出现过 代码: // 先处理出来每一列可以取的字母,例 ...
- UVa 1262 (第k字典序) Password
题意: 给出两个6行5列的字母矩阵,一个密码满足:密码的第i个字母在两个字母矩阵的第i列均出现. 然后找出字典序为k的密码,如果不存在输出NO 分析: 我们先统计分别在每一列均在两个矩阵出现的字母,然 ...
- UVA 213 信息解码(二进制&位运算)
题意: 出自刘汝佳算法竞赛入门经典第四章. 考虑下面的01串序列: 0, 00, 01, 10, 000, 001, 010, 011, 100, 101, 110, 0000, 0001, …, 1 ...
- UVa 902 - Password Search
题目:给你一个小写字母组成大的串和一个整数n.找到里面长度为n出现最频繁的子串. 分析:字符串.hash表.字典树. 这里使用hash函数求解,仅仅做一次扫描就可以. 说明:假设频率同样输出字典序最小 ...
- 紫书 例题 10-8 UVa 1262 (暴力枚举)
递归一遍遍历所有情况就ok了 #include<cstdio> #include<cstring> #define REP(i, a, b) for(int i = (a); ...
随机推荐
- codeforces 675 C ——Money Transfers——————【思维题】
Money Transfers time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 【转】WEB安全之渗透测试流程
熟悉渗透流程,攻击会像摆积木一样简单! 0x 01:信息收集 收集网站信息对渗透测试非常重要,收集到的信息往往会让你在渗透中获得意外惊喜. 1. 网站结构 可以使用扫描工具扫描目录,主要扫出网站管理员 ...
- [转]Entity Framework Fluent API - Configuring and Mapping Properties and Types
本文转自:https://msdn.microsoft.com/en-us/data/jj591617#1.2 When working with Entity Framework Code Firs ...
- Swift可选链
//可选链测试 class Person{ var residence:Residence! var name:String init(name:String){ self.name = name } ...
- 深入Java关键字null
一.null是代表不确定的对象 Java中,null是一个关键字,用来标识一个不确定的对象.因此可以将null赋给引用类型变量,但不可以将null赋给基本类型变量. 比如:int a = nu ...
- Oracle OCI操作UDT相关学习(二)
沿用 Oracle OCI操作UDT相关学习 一文中定义的类型和表. 1.更改数据 在sqldeveloper 中更新数据, update dxl.cust set addr.street='a11' ...
- ssm框架文件上传
有两种方法 导包和上传配置自己搞: 第一种: 上传单个文件: @RequestMapping("/addfile1") public String addfile(@Request ...
- Spring课程 Spring入门篇 7-2 Advice定义及实例
1 解析 1.1 通知:after和afterreturning的区别 1.2 @RunWith 是什么? 2 代码演练 2.1 注解方式配置通知的两种方式 2.2 异常通知 2.3 非异常通知 1 ...
- linux php多版本
ecshop还非php5.2 解压 gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1 打上php-fpm补丁再安装php ...
- mysql创建用户授权
服务器上好多库,为了不让某用户误操作别人的数据库,给他单独建个用户并授权. insert into mysql.user(Host,User,Password, ssl_cipher, x509_is ...