문제 링크: https://www.acmicpc.net/problem/1764
백준 집합과 맵 5단계 - 1764번 듣보잡을 풀어보았다.
풀이: 두 명단에서 겹치는 사람의 이름을 정렬해 출력한다.
C++의 경우, map을 이용했다.
C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
int n,m,cnt=0;
string s;
cin>>n>>m;
map<string,bool>nv;
vector<string>res;
for(int i=0;i<n;i++)
{
cin>>s;
nv.insert(make_pair(s,true));
}
for(int i=0;i<m;i++)
{
cin>>s;
if(nv[s])
{
cnt++;
res.push_back(s);
}
}
sort(res.begin(),res.end());
cout<<cnt<<"\n";
for(int i=0;i<res.size();i++)
{
cout<<res[i]<<"\n";
}
}
Python
n,m=map(int,input().split())
name={input() for i in range(n)}
res=[] # 정답재출용
for i in range(m):
a=input()
if a in name: res.append(a) # 듣도 보도 못하면 넣기
res.sort()
print(len(res))
for i in range(len(res)):
print(res[i])
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
서로 다른 부분 문자열의 개수 풀이 (0) | 2023.01.09 |
---|---|
대칭 차집합 풀이 (0) | 2023.01.09 |
숫자 카드 2 풀이 (0) | 2023.01.06 |
나는야 포켓몬 마스터 이다솜 풀이 (0) | 2023.01.06 |
문자열 집합 풀이 (0) | 2023.01.06 |