java implements the sixth Blue Bridge Cup ciphertext search

Posted by TubeRev on Wed, 31 Jul 2019 02:25:20 +0200

Ciphertext Search

Holmes received a message from Planet X, all in lowercase letters.
His assistant provided another piece of information: many password lists of 8 lengths.
Holmes found that the codes were disrupted and hidden in the previous information.

Please write a program to search for possible hidden passwords from the first data. Consider all possible permutations of passwords.

Data format:

Input the first line: a string s, all composed of lowercase letters, less than 1024*1024 in length
The next line is an integer n, which means that the following line has an n-line password, 1<=n<=1000
Next comes the n-line string, all lowercase letters, 8 in length.

Require output:
An integer representing the total number of matches in s for all permutations of passwords per line.

For example:
User input:
aaaabbbbaabbcccc
2
aaaabbbb
abcabccc

Then the program should output:
4

This is because: the first password matches three times, the second password matches one time, a total of four times.

Resource agreements:
Peak memory consumption (including virtual machines) < 512M
CPU consumption < 5000ms

Please output strictly according to the requirements, and do not print something like "Please input..." Excess content.

All the code is placed in the same source file. After debugging is passed, the source code is copied and submitted.
Note: Do not use package statements. Do not use features of jdk1.7 or above.
Note: The name of the main class must be Main, otherwise it will be treated as invalid code.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    
    public void getResult(String A, String[] pwd) {
        int count = 0;
        ArrayList<Integer> list = new ArrayList<Integer>();
        int[] num = new int[pwd.length];
        for(int i = 0;i < pwd.length;i++) {
            int hash = pwd[i].hashCode();
            if(list.contains(hash)) {
                int j = list.indexOf(hash);
                num[j]++;
            } else {
                list.add(hash);
                num[list.size() - 1]++;
            }
        }
        for(int i = 0;i <= A.length() - 8;i++) {
            String s = A.substring(i, i + 8);
            char[] temp = s.toCharArray();
            Arrays.sort(temp);
            StringBuffer t = new StringBuffer("");
            for(int j = 0;j < 8;j++)
                t.append(temp[j]);
            s = t.toString();
            int hash = s.hashCode();
            if(list.contains(hash)) {
                int k = list.indexOf(hash);
                count = count + num[k];
            }
        }
        System.out.println(count);
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        String A = in.next();
        int n = in.nextInt();
        String[] pwd = new String[n];
        for(int i = 0;i < n;i++) {
            char[] arrayP = in.next().toCharArray();
            Arrays.sort(arrayP);
            StringBuffer s = new StringBuffer("");
            for(int j = 0;j < arrayP.length;j++)
                s.append(arrayP[j]);
            pwd[i] = s.toString();
        }
        test.getResult(A, pwd);
    }
}

Topics: Java less