Skip to content

L1-020 帅到没朋友

当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友。本题就要求你找出那些帅到没有朋友的人。

输入格式:

输入第一行给出一个正整数N(≤100),是已知朋友圈的个数;随后N行,每行首先给出一个正整数K(≤1000),为朋友圈中的人数,然后列出一个朋友圈内的所有人——为方便起见,每人对应一个 ID 号,为 5 位数字(从 00000 到 99999),ID 间以空格分隔;之后给出一个正整数M(≤10000),为待查询的人数;随后一行中列出M个待查询的 ID,以空格分隔。

注意:没有朋友的人可以是根本没安装“朋友圈”,也可以是只有自己一个人在朋友圈的人。虽然有个别自恋狂会自己把自己反复加进朋友圈,但题目保证所有K超过 1 的朋友圈里都至少有 2 个不同的人。

输出格式:

按输入的顺序输出那些帅到没朋友的人。ID 间用 1 个空格分隔,行的首尾不得有多余空格。如果没有人太帅,则输出No one is handsome

注意:同一个人可以被查询多次,但只输出一次。

输入样例 1:

tex
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
8
55555 44444 10000 88888 22222 11111 23333 88888
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
8
55555 44444 10000 88888 22222 11111 23333 88888

输出样例 1:

tex
10000 88888 23333
10000 88888 23333

输入样例 2:

3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
4
55555 44444 22222 11111
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
4
55555 44444 22222 11111

输出样例 2:

No one is handsome
No one is handsome

Solution:

WARNING

测试点 4 运行超时

java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(in.readLine());
        Set<String> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            String[] input = in.readLine().split(" ");
            if (input.length > 2) {
                for (int j = 1; j < input.length; j++) set.add(input[j]);
            }
        }
        int m = Integer.parseInt(in.readLine());
        StringBuilder sb = new StringBuilder();
        String[] input = in.readLine().split(" ");
        in.close();
        for (String s : input) {
            if (!set.contains(s)) {
                sb.append(s).append(" ");
                set.add(s);
            }
        }
        if (sb.length() == 0) System.out.println("No one is handsome");
        else System.out.println(sb.toString().trim());
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(in.readLine());
        Set<String> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            String[] input = in.readLine().split(" ");
            if (input.length > 2) {
                for (int j = 1; j < input.length; j++) set.add(input[j]);
            }
        }
        int m = Integer.parseInt(in.readLine());
        StringBuilder sb = new StringBuilder();
        String[] input = in.readLine().split(" ");
        in.close();
        for (String s : input) {
            if (!set.contains(s)) {
                sb.append(s).append(" ");
                set.add(s);
            }
        }
        if (sb.length() == 0) System.out.println("No one is handsome");
        else System.out.println(sb.toString().trim());
    }
}

Released under the MIT License.