Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ll long long
- #include <bits/stdc++.h>
- using namespace std;
- const int OO = 1e9;
- const double EPS = 1e-9;
- string out[501];
- void conc(int d, char c, int oi) {
- if(oi < out[d].length()) {
- out[d][oi] = c;
- }
- else {
- out[d] += c;
- }
- }
- class Trie {
- map<char,Trie*> mp;
- bool flag;
- public:
- Trie() {
- flag = 0;
- }
- ~Trie() {
- for(pair<char,Trie*> p : mp) {
- delete p.second;
- }
- }
- void add(string &s, int idx = 0) {
- if(s.length() == idx) {
- flag = 1;
- return;
- }
- if(mp.find(s[idx]) == mp.end()) {
- mp[s[idx]] = new Trie();
- }
- if(s[idx] == '\\') {
- flag = 1;
- }
- mp[s[idx]]->add(s,idx+1);
- }
- void printAll(int oi = 0, int d = 0) {
- if(flag) {
- cout << string(d, ' ') << out[d].substr(0,oi) << "\n";
- }
- if(mp['\\']) {
- //cout << string(d, ' ') << out[d].substr(0,oi) << "\n";
- //cout << "moved1 to" << p.first << " out is " << out << "\n";
- mp['\\']->printAll(0,d+1);
- }
- for(pair<char,Trie*> p : mp) {
- if(p.first == '\\') {
- continue;
- }
- else {
- conc(d,p.first,oi);
- //cout << "moved2 to" << p.first << " out is " << out << "\n";
- p.second->printAll(oi+1,d);
- }
- }
- }
- };
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- //freopen("out.txt","w",stdout);
- int n;
- while(cin >> n) {
- Trie dict;
- for(int i = 0; i < n; i++) {
- string s;
- cin >> s;
- dict.add(s);
- }
- dict.printAll();
- cout << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement