[BAEKJOON] 22233번 가희와 키워드

2024. 3. 25. 19:38·Algorithm/구현, 시뮬레이션

https://www.acmicpc.net/problem/22233

 

22233번: 가희와 키워드

1번째 글을 쓰고 난 후에, 메모장에 있는 키워드는 set, floyd, os가 됩니다. 2번째 글을 쓰고 난 후에, 메모장에 있는 키워드는 set, os가 됩니다. map은 1번째 글과 2번째 글에 중복으로 등장하였음을

www.acmicpc.net

 

문제조건
  1. 메모장에 키워드가 적혀있다.(중복X)
  2. 블로그에 글을쓰면 메모장에서 사라진다.
  3. 키워드가 , (쉼표)로 구분해서 주어진다.
  4. 블로그에 전부 글을 쓴뒤 메모장에 남은 키워드의 개수는?

키워드를 미리 map에 저장해두고, ","(콤마)를 만날때마다 단어를 만들어서 map에 있는 단어인지 체크만 하는 간단한 구현문제이다.

#include <iostream>
#include <vector>
#include <map>
using namespace std;

int n, m;
int main(){
	cin >> n >> m;
	map<string, int> memo;
	int cnt = 0;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		memo.insert({ s, 1 });
		cnt++;
	}
	for (int i = 0; i < m; i++) {
		string s;
		cin >> s;
		string w = "";
		for (int j = 0; j < s.length(); j++) {
			if (s[j] == ',') {
				if (memo[w] == 1) {
					cnt--;
					memo[w] = 0;
				}
				w = "";
			}
			else {
				w += s[j];
				if (j == s.length() - 1) {
					if (memo[w] == 1) {
						cnt--;
						memo[w] = 0;
					}
				}
			}
		}
		cout << cnt << '\n';
	}

}

여기서 내가 시간초과가 난 이유를 찾아본 결과, 자료구조로 map을 사용했기 때문이다. 

중복되는 키워드에 대해서까지 고려하기 때문에 시간초과가 났다.

따라서 자료구조를 unordered_set으로 바꿔주고, 다시 풀었더니,,,

#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

int n, m;
int main(){
	ios::sync_with_stdio(false);
	cout.tie(NULL);
	cin.tie(NULL);

	cin >> n >> m;
	unordered_set<string> memo;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		memo.insert(s);
	}
	for (int i = 0; i < m; i++) {
		string s;
		cin >> s;
		string w = "";
		for (int j = 0; j < s.length(); j++) {
			if (s[j] == ',') {
				if (memo.find(w) != memo.end()) {
					memo.erase(w);
				}
				w = "";
			}
			else {
				w += s[j];
				if (j == s.length() - 1) {
					if (memo.find(w) != memo.end()) {
						memo.erase(w);
					}
				}
			}
		}
		cout << memo.size() << '\n';
	}

}

'Algorithm > 구현, 시뮬레이션' 카테고리의 다른 글

[BAEKJOON] 13460번 구슬 탈출 2  (0) 2024.04.05
[BAEKJOON] 20006번 랭킹전 대기열  (0) 2024.04.01
[BAEKJOON] 8979번 올림픽  (0) 2024.03.20
[BAEKJOON] 10431번 줄세우기  (1) 2024.03.19
[BAEKJOON] 9655번 돌게임  (1) 2024.03.19
'Algorithm/구현, 시뮬레이션' 카테고리의 다른 글
  • [BAEKJOON] 13460번 구슬 탈출 2
  • [BAEKJOON] 20006번 랭킹전 대기열
  • [BAEKJOON] 8979번 올림픽
  • [BAEKJOON] 10431번 줄세우기
Ls._.Rain
Ls._.Rain
안되면 될때까지 삽질했던 기록
  • Ls._.Rain
    Ls{Diary}
    Ls._.Rain
  • 전체
    오늘
    어제
    • 분류 전체보기 (136)
      • Github (2)
      • Spring (51)
        • Batch Programming (13)
        • 결제 (4)
        • 대용량 트래픽 (32)
        • OpenAI (0)
        • Security (0)
        • WebSocket (0)
        • JPA (1)
      • Algorithm (67)
        • DFS (6)
        • BFS (6)
        • Dynamic Programming (10)
        • Brute Force (4)
        • Binary Search (6)
        • 구현, 시뮬레이션 (15)
        • Stack (1)
        • Greedy (4)
        • Priority_Queue (2)
        • Back Tracking (3)
        • Geometry (2)
        • SCC (1)
        • 투포인터 (4)
        • 최대유량 (1)
        • 정렬 (1)
      • OS (0)
      • DevOps (15)
        • AWS (11)
        • Docker (4)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • hELLO· Designed By정상우.v4.10.0
Ls._.Rain
[BAEKJOON] 22233번 가희와 키워드
상단으로

티스토리툴바