POJ3283+字典树

时间:2023-12-19 22:14:32

简单的字典树

 /*
字典树
构造字典树。注意初始化!
*/
#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 = ;
const int maxm = ;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-; int a[ maxm ];
struct tree{
tree* next[ maxn ];
//bool lev;
};
tree root;
int tot; int getval(char mod[],char aim)
{
int i;
for(i=;mod[i];i++)
{
if(mod[i]==aim)return i;
}
return -;
} int getval(char s[])
{
char value[]="A234567891JQK";
char suit[]="CDHS";
int a=getval(value,s[]);
int len=strlen(s);
int b=getval(suit,s[len-]);
return a*+b;
} void init(){
tot = ;
for( int i=;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=;i<=n;i++ ){
if( p->next[ a[i] ]==NULL ){
tmp = ( tree* )malloc( sizeof( root ) );
//tmp->lev = true;
for( int j=;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> ){
int n;
init();
char tmp[ ];
for( int i=;i<=m;i++ ){
scanf("%d",&n);
//printf("i=%d\n",i);
for( int j=n;j>=;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 ;
}