728x90
반응형
Sort
+
Map
- 제목
Anagram
- 조건
시간 제한 : 4 초
메모리 제한 : 256 MB
- 문제
Two words are anagrams if the letters of the first word can be reordered to obtain the second one. An instance of anagrams is the pair “listen” and “silent”.
You are given a list of words, each word consisting of lowercase letters. Your goal is to filter this list by dropping any word whose anagram has already appeared earlier on the list.
- 입력
The first line contains $n$, the length of the list. This is followed by n lines, each containing one word.
- 출력
Print out the list of words without anagrams, one word per line. The words should appear in the same order as given in the input.
예제 입력1 | 예제 출력1 |
5 listen santa satan silent cat |
listen santa cat |
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
int main() {
fastio;
string word, sorted;
map<string, string> dict;
queue<string> que;
int sz;
cin >> sz;
while(sz--){
cin >> word;
sorted = word;
sort(sorted.begin(), sorted.end());
if(dict.find(sorted) == dict.end()) {
dict.insert({sorted, word});
que.push(word);
}
}
while(!que.empty()){
cout << que.front() << endl;
que.pop();
}
return 0;
}
728x90
반응형
'Problem Solving > BaekJoon' 카테고리의 다른 글
[BOJ/백준] 1193 - 분수찾기 (0) | 2022.09.16 |
---|---|
[BOJ/백준] 6566 - 애너그램 그룹 (0) | 2022.09.16 |
[BOJ/백준] 1407 - 2로 몇 번 나누어질까 (0) | 2022.09.15 |
[BOJ/백준] 16600 - Contemporary Art (0) | 2022.09.15 |
[BOJ/백준] 25427 - DKSH를 찾아라 (0) | 2022.09.13 |