HDU 1075 1247(字典树)

来源:互联网 发布:网络代维管理系统下载 编辑:程序博客网 时间:2024/05/19 21:19

1075

/* 字典树 hdu 1075   火星文翻译*/#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;const int MAX = 26;struct node{    int count ;//记录到此是否是某个火星词的结尾    char s[15];//如果是某个火星词的结尾则存储对应的英文单词    struct node *next[MAX];};struct node *root;struct node *build(){    node *p;    p=(struct node *)malloc(sizeof(struct node));    for(int i = 0; i < 26; i++)    {        p->next[i] = NULL;    }    p->count = -1;    return p;}void Insert(char *ch,char *s){    int len=strlen(s);    if(len==0)return ;    node *p;    p = root;    for(int i = 0; i < len; i++)    {        if(p->next[s[i]-'a']!=NULL)        {            p=p->next[s[i]-'a'];        }        else        {            p->next[s[i]-'a']=build();            p=p->next[s[i]-'a'];        }    }    p->count = 1;    strcpy(p->s,ch);}int Query(char *s){    struct node *p;    int len = strlen(s);    if(len==0)return 0;    p = root;    for(int i=0;i<len ;i++)    {        if(p->next[s[i]-'a']!=NULL)            p=p->next[s[i]-'a'];        else            return 0;    }    if(p->count==1)    {        printf("%s",p->s);        return 1;    }    return 0;//这个地方要注意,该单词有可能只是某个词的前缀}int main(){    char str[15],str1[15],str2[15],str3[3005];    root = build();    gets(str);    while(scanf("%s",str1) && strcmp(str1,"END")!=0)    {        getchar();        scanf("%s",str2);        getchar();        Insert(str1,str2);    }    getchar();    gets(str2);    gets(str3);    while(strcmp(str3,"END")!=0)    {        int len = strlen(str3);        for(int i = 0; i < len;)        {            if(str3[i] < 'a' || str3[i] > 'z')            {                printf("%c",str3[i]);                i++;            }            else            {                int t = 0;                while(str3[i]>='a'&&str3[i]<='z')                {                    str1[t++] = str3[i];                    i++;                }                str1[t] = '\0';                if(Query(str1)==0)printf("%s",str1);            }        }        printf("\n");        gets(str3);    }    return 0;}

1247

/* 字典树 hdu 1247   判断某个单词是不是由给出的单词表中的任意两个单词组合成的*/#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <stack>#include <algorithm>using namespace std;const int MAX = 26;struct node{    int count ;//记录到此是否是某个词的结尾    struct node *next[MAX];};struct node *root;struct node *build(){    node *p;    p=(struct node *)malloc(sizeof(struct node));    for(int i = 0; i < 26; i++)    {        p->next[i] = NULL;    }    p->count = 0;    return p;}void Insert(char *s){    int len=strlen(s);    if(len==0)return ;    node *p;    p = root;    for(int i = 0; i < len; i++)    {        if(p->next[s[i]-'a']!=NULL)        {            p=p->next[s[i]-'a'];        }        else        {            p->next[s[i]-'a']=build();            p = p->next[s[i]-'a'];        }    }    p->count = 1;}int Query(char *s){    int len = strlen(s);    node *p;    p = root;    stack<int > k;    int i = 0;    while(i < len)    {        if(p->next[s[i]-'a']==NULL)return 0;        p = p->next[s[i]-'a'];        if(p->count==1) //找到该单词含有子单词的分隔点        {            k.push(i);        } //入栈        i++;    }    while(!k.empty())    {        int i = k.top() + 1;        k.pop();        node *p = root;        bool flag = true;        while(i < len)        {            if(p->next[s[i]-'a']==NULL)            {                flag = false;                break;            }            p = p->next[s[i]-'a'];            i++;        }        if(flag && p->count==1)//找到最后,并且是单词的结尾        return 1;    }    return 0;}//void Release(node *p)//{//    if(p==NULL)//    return ;//    for(int i = 0; i < MAX; i++)//    {//        if(p->next[i]!=NULL)//        Release(p->next[i]);//    }//    free(p);//    return ;//}int main(){    char m[50005][20];    int i = 0;    root = build();    while(~scanf("%s",m[i]))    {        Insert(m[i++]);    }    for(int j = 0; j < i; j++)    {        if(Query(m[j])==1)        puts(m[j]);    }    return 0;}


0 0