알고리즘/백준

백준 6973 Dynamic Dictionary Coding

등반 2021. 12. 27. 00:39

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

 

6973번: Dynamic Dictionary Coding

A common method of data compression, "dictionary coding", is to replace words in a text by numbers indicating their positions in a dictionary. Static dictionary coding, in which the dictionary is known in advance, can be problematic, as it is necessary to

www.acmicpc.net

문제 해설

테스트 케이스의 수가 주어지고, 테스트 케이스마다 압축되어야 할 문장이 주어진다.

문장을 읽으며 나오는 단어들에 번호를 붙여 매핑 테이블(사전)을 만든다.

이후에, 이미 한 번 이상 나왔던 단어는 매핑 테이블을 이용해 매핑한 후 문장을 이어간다.

#include <iostream>
#define fio cin.tie(0)->sync_with_stdio(0)
using namespace std;

#include <map>
#include <sstream>
int main(){
    fio;
    int N; cin >> N; cin.ignore();

    for(int i=0; i <N; i++){
        map<string, int> map_;
        int idx = 1;
        while(1){
            string S;
            getline(cin, S);
            if(S.size() == 0)   break;
            stringstream ss(S);
            while(ss >> S){
                if(map_.find(S) != map_.end()){
                    cout << map_[S] <<' ';
                }else{
                    cout << S <<' ';
                    map_[S] = idx++;
                }
            }
            cout <<'\n';
        }
        cout <<'\n';
    }
    return 0;
}

 

'알고리즘 > 백준' 카테고리의 다른 글

백준 9733 꿀벌  (0) 2021.12.27
백준 8641 Sklep  (0) 2021.12.27
백준 6325 Definite Values  (0) 2021.12.27
백준 5608 問題 2  (0) 2021.12.27
백준 4775 Spelling Be  (0) 2021.12.27