这题不难,用遍历就能实现。代码如下。
本以为 没什么问题,可是万万没想到,程序超时了,,倒可以用C再实现一遍,估计不会超时。我研究一下,再更新
import java.io.IOException;
import java.io.InputStreamReader;
public class Main3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char brokens[] = br.readLine().toCharArray();
boolean exit_shift = exit('+', brokens);
//char out[]=new char[20+1];
int temp;
for (int i = 0; (temp = br.read()) != -1; ) {
char a = (char) temp;
if (!((exit_shift && a >= 'A' && a <= 'Z') || exit(a, brokens))) {
System.out.print(a);
}
}
}
static boolean exit(char a, char chars[]) {
for (char c : chars) {
if (c == a) {
return true;
} else if ('A' <= c && 'Z' >= c && (int) (a - c) == 32) {
return true;
}
}
return false;
}
}
用set实现了一遍还是超时。。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.HashSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char brokens[] = br.readLine().toCharArray();
char sentense[]=br.readLine().toCharArray();
boolean exit_shift = exit('+', brokens);
Set<Integer> set=new HashSet();
for (char c : brokens) {
set.add((int)c);
}
for (char a : sentense) {
if (exit_shift&&a>='A'&&a<='Z') {
continue;
}else if (a>='a'&&a<='z') {
if (!set.contains(a+'A'-'a')) {
System.out.print(a);
}
}else if (!set.contains((int)a)) {
System.out.print(a);
}
}
}
static boolean exit(char a, char chars[]) {
for (char c : chars) {
if (c == a) {
return true;
}
}
return false;
}
}