UVA11488 字典树模板题

来源:互联网 发布:sd卡数据恢复手机版 编辑:程序博客网 时间:2024/05/29 16:43

https://vjudge.net/problem/UVA-11488

求   前缀*以这个子串为前缀的数目   的最大值。

字典树模板题。

#include <cstdlib>#include <cstdio>#include <iostream>#include <string>#include <cstring>using namespace std;const int AX =5e5+6;int ans;typedef struct Trie_Node{struct Trie_Node* next[2];int count;bool exist;}TrieNode,*Trie;TrieNode* Trie_createroot(){TrieNode* root = new TrieNode();root->count = 0;root->exist = false;memset(root->next,0,sizeof(root->next));return root;}void Trie_insert(Trie node,char* p){int num = 0;while( *p ){num++;if(node->next[*p-'0'] == NULL){node->next[*p-'0'] = Trie_createroot();}node = node->next[*p-'0'];++p;node->count += 1;ans = max( ans , num*node->count );     //只需要再模板上加个取最值判断}node->exist = true;}int main(){    int T;    cin>>T;    int n;    while( T-- ){    ans = 0;    char s[201];    scanf("%d",&n);    Trie root = Trie_createroot();    for( int i = 0 ; i < n ; i++ ){    scanf("%s",s);    Trie_insert(root,s);    }    printf("%d\n",ans);    }    return 0;}