[C++] 개미굴

[C++] 개미굴

#include < iostream >

#include < map >

#include < vector >

using namespace std ;

class Node{

public :

map < string , Node * > m;

void insert( vector < string > vec, int idx){

if (idx = = vec. size ()){

return ;

}

else {

if ( this - > m.find(vec[idx]) = = this - > m. end ()){

// create new Node

Node * node = new Node;

this - > m[vec[idx]] = node;

}

this - > m.find(vec[idx]) - > second - > insert(vec,idx + 1 );

}

}

void print( int count){

for ( auto it = this - > m. begin () ; it ! = this - > m. end () ; it + + ){

for ( int i = 0 ; i < count * 2 ; i + + ) cout < < "-" ;

cout < < it - > first < < "

" ;

it - > second - > print(count + 1 );

}

}

};

int main(){

int N;

cin > > N;

Node * root = new Node;

for ( int i = 0 ; i < N ; i + + ){

int K;

cin > > K;

vector < string > vec;

for ( int j = 0 ;j < K ; j + + ){

string str;

cin > > str;

vec. push_back (str);

}

root - > insert(vec, 0 );

}

root - > print( 0 );

}

from http://tunsi-niley.tistory.com/94 by ccl(A) rewrite - 2021-08-24 00:00:15