题目地址
最后一个测试点数据量太大了,尝试用BufferedReader减少输入时间,以及数组来储存人物信息来减少查找时间,虽然前三个数据点很快,但是最后一个测试点还是过不了。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class L1_20帅到没朋友 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//方法1. 曾最慢86ms,但还是超时了
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] are=new int[100000];
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++) {
String[] str=br.readLine().split(" ");
for(int j=1;j<str.length;j++) {
if(str.length==2) {
break;
}
are[Integer.parseInt(str[j])]=1;
}
}
int m=Integer.parseInt(br.readLine());
String str1[]=br.readLine().split(" ");
boolean flag=true; //用来控制格式化结果
for(int i=0;i<m;i++) {
if(are[Integer.parseInt(str1[i])] !=1) {
are[Integer.parseInt(str1[i])]=1;
if(flag) {
System.out.print(str1[i]);
flag=false;
}else {
System.out.print(" "+str1[i]);
}
}
}
if(flag) {
System.out.println("No one is handsome");
}
//方法2,但是依然过不了最后一个结点,时间还是超时了,最慢88ms
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// int[] are=new int[100000];
// int n=Integer.parseInt(br.readLine());
// for(int i=0;i<n;i++) {
// String[] str=br.readLine().split(" ");
// for(int j=1;j<str.length;j++) {
// if(str.length==2) {
// break;
// }
// are[Integer.parseInt(str[j])]=1;
// }
// }
// int m=Integer.parseInt(br.readLine());
// String str1[]=br.readLine().split(" ");
// boolean flag=true; //用来控制格式化结果
// for(int i=0;i<m;i++) {
// if(are[Integer.parseInt(str1[i])] !=1) {
// are[Integer.parseInt(str1[i])]=1;
// if(flag) {
// System.out.print(str1[i]);
// flag=false;
// }else {
// System.out.print(" "+str1[i]);
// }
// }
// }
// if(flag) {
// System.out.println("No one is handsome");
// }
}
}