UVA11488 字典树模板题

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

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

字典树模板题。

#include 
#include 
#include 
#include 
#include 
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;
}

你可能感兴趣的:(暑期集训刷题算法复习(新手,),字典树)