第三和第七个用例通不过,已经看很久了,求调
  • 板块P5587 打字练习
  • 楼主Get40w
  • 当前回复6
  • 已保存回复6
  • 发布时间2025/8/29 20:45
  • 上次更新2025/8/30 12:37:32
查看原帖
第三和第七个用例通不过,已经看很久了,求调
1817368
Get40w楼主2025/8/29 20:45
import java.util.Scanner;
import java.util.Stack;

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

        // 存储标准字符串(应该输入的内容)
        StringBuilder standardInput = new StringBuilder();
        String line;

        // 读取标准输入内容,直到遇到EOF
        while (!(line = sc.nextLine()).equals("EOF")) {
            standardInput.append(line).append("\n");
        }

        // 存储实际输入内容(用户的输入)
        StringBuilder actualInput = new StringBuilder();

        // 读取用户实际输入内容,直到遇到EOF
        while (!(line = sc.nextLine()).equals("EOF")) {
            actualInput.append(line).append("\n");
        }

        // 读取时间
        int time = Integer.parseInt(sc.nextLine());

        // 使用栈处理标准输入,得到应该显示的最终文本
        String expectedText = processWithStack(standardInput.toString());
        //System.out.println(expectedText);

        // 使用栈处理实际输入,得到用户实际显示的文本
        String userText = processWithStack(actualInput.toString());
        //System.out.println(userText);

        // 计算匹配的字符数
        int matchCount = countMatchingCharacters(expectedText, userText);

        // 计算KPM值
        int kpm = (int)(matchCount * 60.0 / time+0.5);

        System.out.println(kpm);
    }

    /**
     * 使用栈处理包含退格符的字符串
     * @param input 包含退格符的原始输入
     * @return 处理后的最终字符串
     */
    /**
     * 使用栈处理包含退格符的字符串
     * @param input 包含退格符的原始输入
     * @return 处理后的最终字符串
     */
    private static String processWithStack(String input) {
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            // 如果遇到退格符 '<',且栈不为空且栈顶不是换行符,则弹出栈顶元素
            if (c == '<') {
                if (!stack.isEmpty() && stack.peek() != '\n') {
                    stack.pop();
                }
            } else {
                // 否则将字符压入栈中
                stack.push(c);
            }
        }

        // 将栈中的字符转换为字符数组
        char[] result = new char[stack.size()];
        for (int i = 0; i < result.length; i++) {
            result[i] = stack.get(i);
        }

        // 返回处理后的字符串
        return new String(result);
    }

    /**
     * 计算两个字符串中匹配的字符数
     * @param expected 期望的文本
     * @param actual 实际的文本
     * @return 匹配的字符数量
     */
    private static int countMatchingCharacters(String expected, String actual) {
        int count = 0;
        int minLength = Math.min(expected.length(), actual.length());
        for (int i = 0, j = 0; i < minLength && j < actual.length(); i++, j++) {
            // 如果当前字符匹配且不是换行符,则计数加一
            if (expected.charAt(i) == actual.charAt(j)) {
                if (expected.charAt(i) != '\n')
                    count++;
            } else {
                // 如果期望字符是换行符,则跳过实际字符串中的字符直到换行符
                if (expected.charAt(i) == '\n') {
                    while (actual.charAt(j) != '\n') {
                        j++;
                    }
                    continue;
                }
                // 如果实际字符是换行符,则跳过期望字符串中的字符直到换行符
                if (actual.charAt(j) == '\n') {
                    while (expected.charAt(i) != '\n') {
                        i++;
                    }
                    continue;
                }
            }
        }
        return count;
    }
}
2025/8/29 20:45
加载中...