POJ3283+字典树

简单的字典树 模拟一遍。。


/*
字典树
构造字典树。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<math.h>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b)) 
const int maxn = 53;
const int maxm = 100005;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-8;

int a[ maxm ];
struct tree{
	tree* next[ maxn ];
	//bool lev;
};
tree root;
int tot;

int getval(char mod[],char aim)
{
    int i;
    for(i=0;mod[i];i++)
    {
        if(mod[i]==aim)return i;
    }
    return -1;
}

int getval(char s[])
{
    char value[15]="A234567891JQK";
    char suit[10]="CDHS";
    int a=getval(value,s[0]);
    int len=strlen(s);
    int b=getval(suit,s[len-1]);
    return a*4+b;
}

void init(){
	tot = 0;
	for( int i=0;i<maxn;i++ )
		root.next[i] = NULL;
	//printf("AC=%d\n",getval("AC"));
	//printf("AC=%d\n",getval("AD"));
	//printf("AC=%d\n",getval("AH"));
	//printf("AC=%d\n",getval("AS"));
	//printf("KS=%d\n",getval("KS"));
}

void build( int n ){
	tree *p = &root;
	tree *tmp;
	for( int i=1;i<=n;i++ ){
		if( p->next[ a[i] ]==NULL ){
			tmp = ( tree* )malloc( sizeof( root ) );
			//tmp->lev = true;
			for( int j=0;j<maxn;j++ ){
				tmp->next[j] = NULL;
			}
			p->next[ a[i] ] = tmp;
			p = p->next[ a[i] ];
			tot++;
		} 
		else{
			p = p->next[ a[i] ];
		}
	}
	return ;
}
			 
int main(){
	int m;
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	while( scanf("%d",&m)&&m>0 ){
		int n;
		init();
		char tmp[ 12 ];
		for( int i=1;i<=m;i++ ){
			scanf("%d",&n);
			//printf("i=%d\n",i);
			for( int j=n;j>=1;j-- ){
				scanf("%s",tmp);
				a[ j ] = getval(tmp);
			}
			//for( int j=1;j<=n;j++ )
				//printf("%d ",a[j]);
			//printf("\n");
			build( n );
		}
		printf("%d\n",tot);
		//delete(&root);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值