标签:des style class java ext color
这题就是一个字典树的模板题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 16997 Accepted Submission(s): 7318
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
typedef struct Node
{
    int flag;
    struct Node *next[26];
}Node,*Tree;
int kk=0;
void Creat(Tree &T)
{
    T=(Node *)malloc(sizeof(Node));
    T->flag=0;
    for(int i=0;i<26;i++)
        T->next[i]=NULL;
}
void insert(Tree &T,char *s)
{
    Tree p=T;
    int l=strlen(s);
    int t;
    for(int i=0;i<l;i++)
    {
        t=s[i]-‘a‘;
        if(p->next[t]==NULL)
            Creat(p->next[t]);
        p=p->next[t];
        p->flag++;
    }
}
int search(Tree &T,char *s)
{
    int t;
    Tree p=T;
    int l=strlen(s);
    for(int i=0;i<l;i++)
    {
        t=s[i]-‘a‘;
        if(p->next[t]==NULL)
            return 0;
        p=p->next[t];
    }
    return p->flag;
}
void Delete(Tree p)
{
    for(int i=0;i<26;i++)
     if(p->next[i]!=NULL)
      Delete(p->next[i]);
    free(p);
}
int main()
{
    char str[20];
    char a[20];
    Tree T;
    Creat(T);
    while(gets(a)&&a[0])
    {
        insert(T,a);
    }
    int tt;
    while(scanf("%s%*c",str)!=EOF)
    {
        tt=search(T,str);
        printf("%d\n",tt);
    }
    Delete(T);
    return 0;
}
标签:des style class java ext color
原文地址:http://www.cnblogs.com/zhangmingcheng/p/3806725.html