注意
第三个测试点容易出错,题目说的是4位数字,所以输出的时候就必须是四位,即读入的是0721,不注意的话,会直接输出721,这样测试点就不通过。
2021.04.13
#include <iostream>
#include <unordered_set>
using namespace std;
int main ( ) {
int n, m;
scanf ( "%d %d" , & n, & m) ;
unordered_set< int > st;
int a;
for ( int i = 0 ; i < m; i++ ) {
scanf ( "%d" , & a) ;
st. insert ( a) ;
}
int totalStu = 0 , total = 0 ;
char name[ 16 ] ;
int cnt = 0 ;
for ( int i = 0 ; i < n; i++ ) {
scanf ( "%s %d" , name, & cnt) ;
bool first = true ;
for ( int j = 0 ; j < cnt; j++ ) {
scanf ( "%d" , & a) ;
if ( st. count ( a) ) {
total++ ;
if ( first) {
printf ( "%s: %04d" , name, a) ;
first = false ;
} else {
printf ( " %04d" , a) ;
}
}
}
if ( ! first) {
totalStu++ ;
printf ( "\n" ) ;
}
}
printf ( "%d %d" , totalStu, total) ;
return 0 ;
}
C++
#include <cstdio>
#include <cstring>
const int maxn= 10005 ;
int main ( ) {
int N, M, num, k;
char name[ 5 ] ;
scanf ( "%d%d" , & N, & M) ;
int item[ maxn] ;
memset ( item, 0 , sizeof ( item) ) ;
for ( int i= 0 ; i< M; i++ ) {
scanf ( "%d" , & num) ;
item[ num] = 1 ;
}
int cntp= 0 , cntt= 0 ;
for ( int i= 0 ; i< N; i++ ) {
scanf ( "%s %d" , name, & k) ;
int tag= 1 ;
for ( int j= 0 ; j< k; j++ ) {
scanf ( "%d" , & num) ;
if ( item[ num] == 1 ) {
cntt++ ;
if ( tag== 1 ) {
printf ( "%s:" , name) ;
tag= 2 ;
}
printf ( " %04d" , num) ;
}
}
if ( tag== 2 ) {
cntp++ ;
printf ( "\n" ) ;
}
}
printf ( "%d %d" , cntp, cntt) ;
return 0 ;
}