POJ3283+字典树

简单的字典树

  1 /*
  2 字典树
  3 构造字典树。注意初始化!
  4 */
  5 #include<stdio.h>
  6 #include<string.h>
  7 #include<stdlib.h>
  8 #include<algorithm>
  9 #include<iostream>
 10 #include<queue>
 11 #include<map>
 12 #include<stack>
 13 #include<set>
 14 #include<math.h>
 15 using namespace std;
 16 typedef long long int64;
 17 //typedef __int64 int64;
 18 typedef pair<int64,int64> PII;
 19 #define MP(a,b) make_pair((a),(b)) 
 20 const int maxn = 53;
 21 const int maxm = 100005;
 22 const int inf = 0x7fffffff;
 23 const double pi=acos(-1.0);
 24 const double eps = 1e-8;
 25 
 26 int a[ maxm ];
 27 struct tree{
 28     tree* next[ maxn ];
 29     //bool lev;
 30 };
 31 tree root;
 32 int tot;
 33 
 34 int getval(char mod[],char aim)
 35 {
 36     int i;
 37     for(i=0;mod[i];i++)
 38     {
 39         if(mod[i]==aim)return i;
 40     }
 41     return -1;
 42 }
 43 
 44 int getval(char s[])
 45 {
 46     char value[15]="A234567891JQK";
 47     char suit[10]="CDHS";
 48     int a=getval(value,s[0]);
 49     int len=strlen(s);
 50     int b=getval(suit,s[len-1]);
 51     return a*4+b;
 52 }
 53 
 54 void init(){
 55     tot = 0;
 56     for( int i=0;i<maxn;i++ )
 57         root.next[i] = NULL;
 58     //printf("AC=%d\n",getval("AC"));
 59     //printf("AC=%d\n",getval("AD"));
 60     //printf("AC=%d\n",getval("AH"));
 61     //printf("AC=%d\n",getval("AS"));
 62     //printf("KS=%d\n",getval("KS"));
 63 }
 64 
 65 void build( int n ){
 66     tree *p = &root;
 67     tree *tmp;
 68     for( int i=1;i<=n;i++ ){
 69         if( p->next[ a[i] ]==NULL ){
 70             tmp = ( tree* )malloc( sizeof( root ) );
 71             //tmp->lev = true;
 72             for( int j=0;j<maxn;j++ ){
 73                 tmp->next[j] = NULL;
 74             }
 75             p->next[ a[i] ] = tmp;
 76             p = p->next[ a[i] ];
 77             tot++;
 78         } 
 79         else{
 80             p = p->next[ a[i] ];
 81         }
 82     }
 83     return ;
 84 }
 85              
 86 int main(){
 87     int m;
 88     //freopen("in.txt","r",stdin);
 89     //freopen("out.txt","w",stdout);
 90     while( scanf("%d",&m)&&m>0 ){
 91         int n;
 92         init();
 93         char tmp[ 12 ];
 94         for( int i=1;i<=m;i++ ){
 95             scanf("%d",&n);
 96             //printf("i=%d\n",i);
 97             for( int j=n;j>=1;j-- ){
 98                 scanf("%s",tmp);
 99                 a[ j ] = getval(tmp);
100             }
101             //for( int j=1;j<=n;j++ )
102                 //printf("%d ",a[j]);
103             //printf("\n");
104             build( n );
105         }
106         printf("%d\n",tot);
107         //delete(&root);
108     }
109     return 0;
110 }
View Code

 

posted @ 2013-08-19 19:13  xxx0624  阅读(165)  评论(0编辑  收藏  举报