-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdictree.cpp
46 lines (37 loc) · 821 Bytes
/
dictree.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<string> cuv;
size_t ndCnt = 0;
void Solve(size_t lvl, size_t st, size_t fn) {
unsigned nSt = st;
while(nSt < fn && cuv[nSt].size()<=lvl) nSt++;
for(size_t i = nSt; i < fn-1; ++i) {
if(cuv[i][lvl] != cuv[i+1][lvl]) {
Solve(lvl+1,nSt,i+1);
ndCnt++;
nSt = i+1;
}
}
if(nSt < fn) {
Solve(lvl+1,nSt,fn);
ndCnt++;
}
}
int main() {
size_t n;
ifstream in("dictree.in");
in >> n;
cuv.resize(n);
for(size_t i = 0; i < n; ++i) {
in >> cuv[i];
}
ofstream out("dictree.out");
sort(cuv.begin(),cuv.end());
out.flush();
Solve(0,0,n);
out << ndCnt+1 << '\n';
}