본문 바로가기

Algorithm/SWEA

[SWEA] 2948번 문자열 교집합 (C++)

728x90
반응형
#include<iostream>
#include<unordered_set>
using namespace std;

unordered_set<string> s;

int main(int argc, char** argv)
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
    
	int test_case;
	int T;
	cin>>T;
	for(test_case = 1; test_case <= T; ++test_case)
	{
		int n, m;
		cin >> n >> m;
		int size = n + m;
		for(int i = 0; i < size; i++){
			string str;
			cin >> str;
			s.insert(str);
		}
		cout << "#" << test_case << " " << size - s.size() << '\n';
        
		s.clear();
	}
	return 0;
}

 

두 집합에 모두 속하는 문자열 원소 개수 = 총 문자열 원소 개수(n + m) - set으로 중복을 제거한 문자열 원소 개수

 

이 때 set STL을 쓰면 시간 초과가 발생한다. set은 자동정렬 기능이 있어서 시간이 오래 걸리기 때문에 정렬할 필요가 없을 경우 unordered_set을 사용해서 시간 줄이기!!

728x90
반응형

'Algorithm > SWEA' 카테고리의 다른 글

[SWEA] 3233번 정삼각형 분할 놀이 (C++)  (0) 2023.11.18
[SWEA] 7532번 세영이의 SEM력 연도 (C++)  (0) 2023.11.16