UVa 1262 - Password(解码)

本文介绍了一个UVA在线评测的经典解码问题。题目要求根据两个6行5列的字母矩阵,找出字典序第k小的密码。文章通过分析给出了具体的解题思路,并提供了Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:

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。

 

分析:

经典的解码问题。首先求出答案每一列的所有可能字母,然后逐位解码即可。
具体实现见代码。

 

代码:

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class Main {
 5     static char G[][][] = new char[2][6][9];
 6     
 7     public static void main(String args[]) {
 8         Scanner cin = new Scanner(new BufferedInputStream(System.in));
 9         
10         int T = cin.nextInt();
11         while(T --> 0) {
12             int k = cin.nextInt();
13             cin.nextLine(); // 清空缓存区
14             for(int t = 0; t < 2; t++) {
15                 for(int r = 0; r < 6; r++) {
16                     G[t][r] = cin.nextLine().toCharArray();
17                 }
18             }
19             
20             @SuppressWarnings("unchecked")
21             ArrayList<Character> a[] = new ArrayList[5]; // a[c]: 答案第c列的所有可能字母
22             for(int i = 0; i < 5; i++) a[i] = new ArrayList<Character>();
23             
24             boolean has[][][] = new boolean[2][5][26]; // 第t个矩阵的第c列是否有字母ch
25             for(int c = 0; c < 5; c++) {
26                 for(int t = 0; t < 2; t++) {
27                     for(int r = 0; r < 6; r++) {
28                         int ch = G[t][r][c] - 'A';
29                         has[t][c][ch] = true;
30                     }
31                 }
32                 for(int i = 0; i < 26; i++)
33                     if(has[0][c][i] && has[1][c][i]) a[c].add((char)(i+'A'));
34             }
35             
36             int up = 1;
37             for(int i = 0; i < 5; i++) up *= a[i].size();
38             if(k > up) {
39                 System.out.println("NO");
40                 continue;
41             }
42             
43             k--; // 解码
44             for(int i = 0; i < 5; i++) {
45                 up /= a[i].size();
46                 System.out.print(a[i].get(k/up));
47                 k %= up;
48             }
49             System.out.println();
50         }
51         cin.close();
52     }
53 }

 

转载于:https://www.cnblogs.com/hkxy125/p/8921248.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值