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

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

a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra

Sample Output

alien
newborn


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
ord hzi
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值