Skip to content

L1-011 A-B ​

本题要求你计算A−B。不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A−B。

输入格式: ​

输入在 2 行中先后给出字符串A和B。两字符串的长度都不超过 104,并且保证每个字符串都是由可见的 ASCII 码和空白字符组成,最后以换行符结束。

输出格式: ​

在一行中打印出A−B的结果字符串。

输入样例: ​

tex
I love GPLT!  It's a fun game!
aeiou
I love GPLT!  It's a fun game!
aeiou

输出样例: ​

tex
I lv GPLT!  It's  fn gm!
I lv GPLT!  It's  fn gm!

Solution: ​

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        char[] a = in.readLine().toCharArray();
        char[] b = in.readLine().toCharArray();
        StringBuilder sb = new StringBuilder();
        int[] book = new int[128];
        for (char c : b) {
            book[c] = 1;
        }
        for (char c : a) {
            if (book[c] != 1) sb.append(c);
        }
        System.out.println(sb);
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        char[] a = in.readLine().toCharArray();
        char[] b = in.readLine().toCharArray();
        StringBuilder sb = new StringBuilder();
        int[] book = new int[128];
        for (char c : b) {
            book[c] = 1;
        }
        for (char c : a) {
            if (book[c] != 1) sb.append(c);
        }
        System.out.println(sb);
    }
}

Released under the MIT License.