[Little Red Book Real Questions] 0819 Autumn Recruitment Written Test Real Questions 1 and 2

1. Little red words

Xiaohong has to memorize words every day, and then she will record how many words she has memorized each day, and check in the little red book. When Xiaohong memorizes words, if she has memorized i words and memorized a new word i+1 times that she has not memorized, she will memorize the new word.

For example, when she memorized ["you", "thank", "thank"] in sequence, she could remember "you" the first time she memorized the word "you". And since she has already memorized a word, she needs to recite "thank" twice to remember "thank". Now that you know the order of Xiaohong's words, please find out how many words Xiaohong has memorized today.

Input description:
The first line is an integer n (1<=n<=10000). In the next n lines, each line has a string, and the length of each string is guaranteed to be no more than 10.

Output description:
Output an integer indicating how many words she remembered.

Sample input:

5
you
thank
queue
queue
thank

sample output

2

hint:

Xiaohong first memorized the word "you", and because she memorized "queue" twice, she memorized the word "queue". remember.

Solution - simulation

HashMap records the number of memorization times of each word, and uses Set to record the words that have been memorized, and the words that have been memorized are not counted

import java.util.*;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        String[] s = new String[n];
        in.nextLine();
        for(int i = 0; i < n; i++){
    
    
            s[i] = in.nextLine();
        }

        //HashMap记录每个单词的次数
        HashMap<String, Integer> map = new HashMap<>();
        //Set记录已经背过的单词,已经背过的单词不计入
        HashSet<String> set = new HashSet<>();

        int sum = 0;
        for(int i = 0; i < n; i++){
    
    
            //已经背过的单词不计入
            if(set.contains(s[i])){
    
    
                continue;
            }

            //HashMap记录每个单词的次数
            map.put(s[i], map.getOrDefault(s[i],0) + 1);

            //当前记的单词数大于该背的单词数了,记为背过
            if(map.get(s[i]) > sum){
    
    
                sum++;
                set.add(s[i]);
            }
        }

        //System.out.println(map);
        //System.out.println(set);
        System.out.println(sum);
    }
}

2. Xiaohong's palindrome

Xiaohong has a string, she can perform the following operations:

  • split. Split 'w' into 2 'v', 'm' into 2 'n'.

  • Axes piled up. Make 'b' axisymmetric to 'd', 'p' axisymmetric to 'q' and vice versa.

  • Flip. Invert 'b' to 'q', 'd' to 'p', and 'n' to 'u'

After several operations, Xiaohong wants to know whether this string can be turned into a palindrome.

Input description:
Enter an integer T in the first line (1<=T<= 1 0 4 10^4104 ) Indicates the number of inquiries
In the next T lines, enter a character string in each line to indicate inquiries.
The sum of all string lengths does not exceed1 0 5 10^5105

Output description:
output T lines, and each line outputs "YES" or "NO" to indicate whether it can be changed into a palindrome.

Sample input:

5
Wovv
bod
pdd
moom
lalalai

sample output

YES
YES
YES
YES
NO

Solution - simulation

Into a palindrome train of thought:

  • bdqp, uniformly converted to b
  • w, turn to vv
  • n, to u
  • m, converted to uu
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] s = new String[n];
        in.nextLine();
        for (int i = 0; i < n; i++) {
    
    
            s[i] = in.nextLine();
        }

        String[] ans = new String[n];
        Arrays.fill(ans, "NO");


        int index = 0;
        for (String str : s) {
    
    

            //使用一个StringBuilder转换字符串s
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.length(); i++) {
    
    
                char c = str.charAt(i);
                // 如果为bdqp,统一转换为b
                if (c == 'b' || c == 'd' || c == 'q' || c == 'p') {
    
    
                    sb.append('b');
                }
                // 如果为w,转为vv
                else if (c == 'w') {
    
    
                    sb.append('v').append('v');
                }
                // 如果为n,转为u
                else if (c == 'n') {
    
    
                    sb.append('u');
                }
                // 如果为m,转为uu
                else if (c == 'm') {
    
    
                    sb.append('u').append('u');
                }
                else {
    
    
                    sb.append(c);
                }
            }

            String s1 = sb.toString();
            //对每一个字符串首尾开始遍历判断是否回文串
            int i = 0, j = s1.length() - 1;
            boolean flag = true;
            while (i < j) {
    
    
                if (s1.charAt(i) != s1.charAt(j)) {
    
    
                    flag = false;
                }
                i++;
                j--;
            }
            if (flag) {
    
    
                ans[index] = "YES";
            }
            index++;
        }

        //输出结果
        for (String x : ans) {
    
    
            System.out.println(x);
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_44033208/article/details/132445876