字典树练习

字典树

HDU 3460

#include <iostream>
#include <cstring>
#include <string>
#include <string.h>
using namespace std;
char s[60];
int sum=0;
struct tire
{
	tire *next[26];
	tire()
	{
		for(int i=0;i<26;i++){
			next[i]=NULL;
		}
	}
}root;

void insert(char word[])
{
	tire *r=&root;
	for(int i=0;word[i];i++){
		if(r->next[word[i]-'a']==NULL){
			r->next[word[i]-'a']=new tire;
			sum++;
		}
		r=r->next[word[i]-'a'];
	}
}

int main()
{
	int n;
	int len=0;
	int p;
	while(cin>>n){
		len=-0x3f3f3f3f;
		sum=0;
		for(int i=0;i<26;i++){
			root.next[i]=NULL;
		}
		for(int i=1;i<=n;i++){
			cin>>s;
			p=strlen(s);
			len=max(len,p);
			insert(s);
		}
		cout<<sum*2+n-len<<endl;
	}
	return 0;
}
posted @ 2018-11-26 18:18  凌乱风中  阅读(97)  评论(0编辑  收藏  举报