-
Notifications
You must be signed in to change notification settings - Fork 8
/
k-anagrams-1.cpp
executable file
·120 lines (113 loc) · 2.14 KB
/
k-anagrams-1.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
//k-anagrams-1
//method 2 -> using trie
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
struct node{
struct node *child[26];
bool isleaf;
vector<int> list;
};
node* getnode(){
node* pnode = new node();
for(int i=0;i<26;i++){
pnode->child[i] = NULL;
}
pnode->isleaf = false;
}
void insert(node *root, string s, int sindex){
int len = s.length();
node *pnode = root;
for(int i=0;i<len;i++){
int index = s[i]-97;
if(!pnode->child[index]){
pnode->child[index] = getnode();
}
pnode = pnode->child[index];
}
pnode->isleaf = true;
pnode->list.push_back(sindex);
}
void traverse(node *root,vector<string> &vec,vector<int> &countvec){
if(root == NULL) return;
if(root->isleaf){
/*for(int j =0;j<root->list.size();j++){
cout<<vec[j]<<" ";
}*/
//cout<<root->list.size()<<" ";
countvec.push_back(root->list.size());
}
for(int i=0;i<26;i++){
traverse(root->child[i],vec,countvec);
}
}
int main(){
int t,n;
cin>>t;
while(t--){
node * root = getnode();
vector<string> vec;
string s;
cin>>n;
for(int i=0;i<n;i++){
cin>>s;
vec.push_back(s);
sort(s.begin(),s.end());
insert(root,s,i);
}
vector<int> countvec;
traverse(root,vec,countvec);
sort(countvec.begin(),countvec.end());
for(int i=0;i<countvec.size();i++){
cout<<countvec[i]<<" ";
}
cout<<endl;
}
}
/*
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
int main(){
int t,n;
cin>>t;
while(t--){
vector <string> vec;
vector<int> countarr;
cin>>n;
string s;
for(int i=0;i<n;i++){
cin>>s;
vec.push_back(s);
}
map<string,int> m;
for(int i=0;i<vec.size();i++){
sort(vec[i].begin(),vec[i].end());
if(m.find(vec[i]) == m.end()){
m[vec[i]] = 1;
}else{
int x = m[vec[i]];
m[vec[i]] = x+1;
}
}
map<string,int> :: iterator it;
for(it = m.begin();it!=m.end();it++){
countarr.push_back(it->second);
}
sort(countarr.begin(),countarr.end());
for(int i=0;i<countarr.size();i++){
cout<<countarr[i]<<" ";
}
countarr.clear();
m.clear();
vec.clear();
cout<<endl;
}
return 0;
}*/