import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
public class Main{
public static String fun(String a, String b){
char[] arrA = a.toCharArray();
char[] arrB = b.toCharArray();
ArrayList<Character> listA = new ArrayList<>();
ArrayList<Character> listB = new ArrayList<>();
int len1 = arrA.length;
int len2 = arrB.length;
for (int i = 0; i < len1; i++){
listA.add(arrA[i]);
}
for (int j = 0; j < len2; j++){
listB.add(arrB[j]);
}
listA.removeAll(listB);
Character[] ans = new Character[listA.size()];
listA.toArray(ans);
int len3 = ans.length;
char[] newAns = new char[len3];
for (int i = 0; i < len3; i++){
newAns[i] = ans[i];
}
String rtn = new String(newAns);
return rtn;
}
public static void main(String[] args){
// Scanner input = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String a = null;
String b = null;
try {
a = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
try {
b = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(Main.fun(a,b));
}
}
我就知道第一次会超时
然后先把数组长度算出来反而更加耗时了
看这位前辈说是用Java过不去看起来这个是Java里面最优解,但还是有一个点过不去
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String A=in.readLine();
String B=in.readLine();
System.out.print(A.replaceAll("[" + B + "]", ""));
}
}
终于过去了
解法:辣么多ASCII码也在0-255之间,所以用数组标记所有的ASCII码,如果第二个字符出现了这个ACSII码那就标记为1~然后输出的时候当数组对应的那个ASCII为1的时候就跳过不输出
#include<stdio.h>
char a[10001];
char b[10001];
char c[256];
int main() // 推荐
{
gets(a);
gets(b);
int len = strlen(a);
int len1 = strlen(b);
for (int i = 0; i < len1; ++i)
{
c[b[i]] = 1;
}
for (int i = 0; i < len; ++i)
{
if (!c[a[i]]) putchar(a[i]);
}
putchar('\n');
return 0;
}