📄 cipher_sherlock.java
字号:
import java.util.*;
import java.io.*;
public class playfair {
static final int maxSize = 1000;
static Scanner in = new Scanner(System.in);
static char board[][] = new char[5][5];
static char que[] = new char[maxSize];
static boolean done[] = new boolean[30];
public static void main(String args[]) throws Exception {
int test_num = in.nextInt();
in.nextLine();
while (test_num -- > 0) {
String buf = in.nextLine();
int x = 0, y = 0;
int len = buf.length();
for (int i = 0; i < 26; i++) {
done[i] = false;
}
done[9] = true;
for (int i = 0; i < len; i++) {
int c = 0;
if (buf.charAt(i) == ' ')
continue;
if (buf.charAt(i) >= 'a' && buf.charAt(i) <= 'z') {
c = buf.charAt(i) - 'a';
} else {
c = buf.charAt(i) - 'A';
}
if (c == 9)
c = 8;
if (done[c])
continue;
done[c] = true;
board[x][y] = (char) ('A' + c);
y ++;
if (y == 5) {
y = 0;
x++;
}
}
for (int i = 0; i < 26; i++)
if (!done[i]) {
board[x][y] = (char) ('A' + i);
y ++;
if (y == 5) {
y = 0;
x++;
}
}
buf = in.nextLine();
len = buf.length();
int n = 0;
for (int i = 0; i < len; i++) {
if (buf.charAt(i) == ' ')
continue;
int c = 0;
if (buf.charAt(i) >= 'a' && buf.charAt(i) <= 'z')
c = buf.charAt(i) - 'a';
else
c = buf.charAt(i) - 'A';
if (c == 9)
c = 8;
if (n > 0 && que[n - 1] == (char) ('A' + c))
que[n ++] = 'X';
que[n ++] = (char) ('A' + c);
}
if (n % 2 != 0)
que[n ++] = 'X';
for (int i = 0; i < n; i += 2) {
int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
char a = que[i];
char b = que[i + 1];
for (int u = 0; u < 5; u ++)
for (int v = 0; v < 5; v++)
if (board[u][v] == a) {
x1 = u;
y1 = v;
} else if (board[u][v] == b) {
x2 = u;
y2 = v;
}
if (x1 != x2 && y1 != y2) {
System.out.print(board[x1][y2]);
System.out.print(board[x2][y1]);
}
else
if (x1 == x2) {
System.out.print(board[x1][(y1 + 1) % 5]);
System.out.print(board[x2][(y2 + 1) % 5]);
}
else {
System.out.print(board[(x1 + 1) % 5][y1]);
System.out.print(board[(x2 + 1) % 5][y2]);
}
}
System.out.println();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -