代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAX_NODE = 50000*201; const int SIGMA_SIZE = 2; int ans; struct Node { Node* ch[SIGMA_SIZE]; int val; Node() { ch[0] = NULL; ch[1] = NULL; val = 0; } }; struct Trie { Node *root; void init() { root = new Node(); } int idx(char c) { return c - '0'; } void insert(char *s) { Node* u = root; int len = strlen(s); for(int i = 0;i < len;i++) { int c = idx(s[i]); if(u->ch[c] == NULL) { u->ch[c] = new Node(); } u = u->ch[c]; u->val++; } } void solve(Node* u,int len) { ans = max(ans,len*(u->val)); for(int i = 0;i < SIGMA_SIZE;i++) { if(u->ch[i]) solve(u->ch[i],len+1); } delete u; } }trie; char str[222]; int main() { int _; scanf("%d",&_); while(_--) { trie.init(); int n; scanf("%d",&n); for(int i = 0;i < n;i++) { scanf("%s",str); trie.insert(str); } ans = 0; trie.solve(trie.root,0); printf("%d\n",ans); } return 0; }