hdu1247

Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16086    Accepted Submission(s): 5737


Problem 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 hatword hziee word
 

Sample Output
  
  
ahat

hatword

题意:找出能够由两个单词组成的单词。

思路:只要将每个单词拆开来,一个个匹配,如果拆开的两个单词都有则是符合的。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
using namespace std;
int head,tail;
typedef struct node
{
    bool isStr;
    struct node *child[26];

}node,*Node;
Node root;
void insert(char *s)
{
    Node next,temp;
    next=root;
    int n=strlen(s);
    int i;
    for(i=0;i<n;i++)
    {
        int z=s[i]-'a';
        if(next->child[z]==NULL)
        {
            temp=new node;
            for(int j=0;j<26;j++)
            {
                temp->child[j]=NULL;
            }
            temp->isStr=false;
            next->child[z]=temp;

        }
        next=next->child[z];
    }
    next->isStr=true;
}
bool search(char *s)
{
    int i;
    Node next;
    next=root;
    for(i=0;s[i]!='\0';i++)
    {
        if(next->child[s[i]-'a']==NULL)
            return false;
        next=next->child[s[i]-'a'];
    }
    return next->isStr;
}
void release(Node root)
{
    for(int i=0;i<26;i++)
    {
        if(root->child[i]!=NULL)
            release(root->child[i]);
    }
    free(root);
}
char c[50010][50];
int main()
{

    root=new node;
    int i;
    for(i=0;i<26;i++)
        root->child[i]=NULL;
    root->isStr=false;
    int k=0;
    while(scanf("%s",c[k])!=EOF)
    {
        insert(c[k++]);
    }
    for(i=0;i<k;i++)
    {
        int len=strlen(c[i]);
        for(int j=1;j<len-1;j++)
        {
            char temp1[55]={'\0'},temp2[55]={'\0'};
            strncpy(temp1,c[i],j);
            strncpy(temp2,c[i]+j,len-j);
            if(search(temp1)&&search(temp2))
            {
                printf("%s\n",c[i]);
                break;
            }
        }
    }
    release(root);
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值