-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathencoder.cpp
More file actions
34 lines (31 loc) · 810 Bytes
/
Copy pathencoder.cpp
File metadata and controls
34 lines (31 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_map>
using namespace std;
typedef unordered_map<char, int> Node;
typedef pair<int, int> Code;
int main(void)
{
char c;
vector<Node> trie;
vector<Code> vec;
trie.push_back(Node()); //root 0
int cur_pos = 0, item = 0;
do {
// read one character from standard input, whitespace included
c = cin.get();
// not found
if (trie[cur_pos].find(c) == trie[cur_pos].end()) {
trie.push_back(Node());
trie[cur_pos].insert(make_pair(c, trie.size()-1));
vec.push_back(make_pair(cur_pos, int(c)));
cur_pos = 0;
} else {
cur_pos = trie[cur_pos][c];
}
} while (c != EOF);
for (int i=0; i<vec.size(); i++)
cout << vec[i].first << " " << vec[i].second << "\n";
return 0;
}