UVA10391_Compound Words/HDU1247_Hat's Words(字典树)

来源:互联网 发布:淘宝鹊桥入口在哪里 编辑:程序博客网 时间:2024/05/21 14:46
Description

Compound Words

You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.

Input

Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 120,000 words.

Output

Your output should contain all the compound words, one per line, in alphabetical order.

Sample Input

aalienbornlesslienneverneverthelessnewnewbornthezebra

Sample Output

aliennewborn


Description

A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are to find all the hat’s words in a dictionary.
 

Input

Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
Only one case.
 

Output

Your output should contain all the hat’s words, one per line, in alphabetical order.
 

Sample Input

a
ahat
hat
ordhzi
hat
wee
wor
d
 

Sample Output

ahat
hatword
 

解题报告
两题一模一样,连代码都一样。
用了+才 的思路,很神奇的思路,不过仅限解此题。。。
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;struct node{    int v;    node *next[26];};node *newnode(){    node *p=new node;    p->v=0;    for(int i=0; i<26; i++)    {        p->next[i]=NULL;    }    return p;}void insertnode(node *root,char *str){    node *p=root;    int l=strlen(str);    for(int i=0; i<l; i++)    {        int t=str[i]-'a';        if(p->next[t]==NULL)        {            p->next[t]=newnode();        }        p=p->next[t];    }    p->v=1;}void find(node *root,char *str){    int k=0,f=0;    int i,j;    node *p=root;    int l=strlen(str);    for(i=0; i<l; i++)    {        int t=str[i]-'a';        if(p->next[t]==NULL)            return ;        p=p->next[t];        if(p->v==1)//核心代码        {            node *q=root;            for(j=i+1; j<l; j++)            {                int u=str[j]-'a';                if(q->next[u]==NULL)                    break ;                q=q->next[u];                if(q->v==1)                {                    if(str[j+1]=='\0')                    {                        printf("%s\n",str);                        return;                    }                    else continue ;                }            }        }    }}char ch[200000][1000];int main(){    char s[10000];    node *root=newnode();    int i=0;    while(scanf("%s",ch[i])!=EOF)    {        insertnode(root,ch[i]);        i++;    }    for(int j=0; j<i; j++)    {        find(root,ch[j]);    }    return 0;}

还有一个类似暴力的解法。。。
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct node{    int v;    node *next[26];} T[1000000];int t=0;node *newnode(){    node *p=new node;//动态分配    //node *p=&T[t++];//静态分配    p->v=0;    for(int i=0; i<26; i++)        p->next[i]=NULL;    return p;}void insertnode(node *root,char *str)//插入{    node *p=root;    int l=strlen(str);    for(int i=0; i<l; i++)    {        int t=str[i]-'a';        if(p->next[t]==NULL)            p->next[t]=newnode();        p=p->next[t];    }    p->v=1;}int find(node *root,char *str)//查找{    node *p=root;    int l=strlen(str);    for(int i=0; i<l; i++)    {        int t=str[i]-'a';        if(p->next[t]==NULL)            return 0;        p=p->next[t];    }    return p->v;}void freenode(node *root)//释放内存{    node *p=root;    for(int i=0; i<26; i++)        if(p->next[i]!=NULL)            freenode(p->next[i]);    free(p);}char ch[240000][100];char sl[240],sr[240];int main(){    int i=0;    node *root=newnode();    while(~scanf("%s",ch[i]))    {        insertnode(root,ch[i]);        i++;    }    for(int j=0; j<i; j++)        for(int k=1; k<strlen(ch[j]); k++)        {            memset(sl,0,sizeof(sl));            memset(sr,0,sizeof(sr));            strncpy(sl,ch[j],k);            strcpy(sr,ch[j]+k);            int nl=find(root,sl);            int nr=find(root,sr);            if(nl&&nr)            {                printf("%s\n",ch[j]);                break;            }        }    return 0;}



0 0
原创粉丝点击