乙级 - 1033 旧键盘打字 (20分)

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?

输入格式

输入在 2 行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过 105 个字符的串。可用的字符包括字母 [a-z, A-Z]数字 0-9、以及下划线 _(代表空格)、,.-+(代表上档键)。题目保证第 2 行输入的文字串非空。

注意:如果上档键坏掉了,那么大写的英文字母无法被打出。

输出格式

在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。

输入样例
7+IE.
7_This_is_a_test.
输出样例
_hs_s_a_tst
Java 代码
1.(17 分,测试点 4 运行超时)
import java.util.Scanner;

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

        String[] strs = new String[2];
        strs[0] = in.nextLine();
        strs[1] = in.nextLine();

        boolean haveUpper = true;
        if (strs[0].contains("+")) {
            haveUpper = false;
        }

        StringBuilder output = new StringBuilder();

        for (int i = 0, len = strs[1].length(); i < len; i++) {
            char c = strs[1].charAt(i);
            // 把一样的排除掉!
            if (strs[0].indexOf(c) != -1) {
                continue;
            }
//          上档键坏掉 并且 是大写
            if (!haveUpper && (c >= 'A' && c <= 'Z')) {
                continue;
            }
            // 转大写查找
            if (strs[0].indexOf(Character.toUpperCase(c)) != -1) {
                continue;
            }
            output.append(c);
        }
        if (output.toString().equals("")) {
            System.out.println();
        } else {
            System.out.print(output);
        }
        System.out.print("");
    }
}
2.(AC)

采用 BufferedReader 读取速度会比较一些。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] strs = new String[2];
        strs[0] = br.readLine();
        strs[1] = br.readLine();

        boolean haveUpper = true;
        if (strs[0].contains("+")) {
            haveUpper = false;
        }

        StringBuilder output = new StringBuilder();

        for (int i = 0, len = strs[1].length(); i < len; i++) {
            char c = strs[1].charAt(i);
            // 把一样的排除掉!
            if (strs[0].indexOf(c) != -1) {
                continue;
            }
            // 上档键坏掉 并且 是大写
            if (!haveUpper && (c >= 'A' && c <= 'Z')) {
                continue;
            }
            // 转大写查找
            if (strs[0].indexOf(Character.toUpperCase(c)) != -1) {
                continue;
            }
            output.append(c);
        }
        if (output.toString().equals("")) {
            System.out.println();
        } else {
            System.out.print(output);
        }
        System.out.print("");
    }
}
3.(AC)

换一种写法

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String str1 = br.readLine();
        String str2 = br.readLine();

        boolean[] badKey = new boolean[150];
        for (int i = 0, len = str1.length(); i < len; i++) {
            badKey[str1.charAt(i)] = true;
            if (str1.charAt(i) >= 'A' && str1.charAt(i) <= 'Z') {
                badKey[str1.charAt(i) + 32] = true;
            }
        }

        StringBuilder result = new StringBuilder();
        for (int i = 0, len = str2.length(); i < len; i++) {
            if (badKey[str2.charAt(i)]) {
                continue;
            }
            if (badKey['+'] && Character.isUpperCase(str2.charAt(i))) {
                continue;
            }
            result.append(str2.charAt(i));
        }

        System.out.println(result);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值