HDU 1247 Hat’s Words

题目链接 HDU 1247

Hat’s Words

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


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
 

Author
戴帽子的
 

题意:
给一堆单词,找出其中有多少个由另外两个单词组成的单词。

分析:
Trie的应用,先将所有的单词保存到树中,再将每个单词分为两段,如果这两段都在树中,那么这个单词满足条件。

代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
char str[50000][50];

struct Trie         //字典树
{
    int IsWord;     //判断该节点是否为单词
    Trie* next[26];
    Trie() {        //构造函数,每创建一棵字典树时自动调用
        IsWord = 0;
        for(int i = 0; i < 26; i++) next[i] = NULL;
    }
} *Root;

void Insert(char ch[])      //插入一个单词
{
    Trie* p = Root;
    for(int i = 0; ch[i]; i++) {
        int k = ch[i] - 'a';
        if(p->next[k] == NULL) {
            Trie* temp = new Trie;
            p->next[k] = temp;
        }
        p = p->next[k];
    }
    p->IsWord = 1;
}

bool Search(char ch[])      //查找单词是否在树中
{
    Trie* p = Root;
    for(int i = 0; ch[i]; i++) {
        int k = ch[i] - 'a';
        if(p == NULL || p->next[k] == NULL) return false;
        p = p->next[k];
    }
    return p->IsWord;
}

void Delete(Trie* root)     //递归释放内存空间
{
    for(int i = 0; i < 26; i++) {
        if(root->next[i] != NULL)
            Delete(root->next[i]);
    }
    delete root;
}

int main()
{
    Root = new Trie;
    int s = 0;
    while(~scanf("%s", str[s])) {
        Insert(str[s]);
        s++;
    }
    for(int i = 0; i < s; i++) {
        int len = strlen(str[i]);
        for(int j = 1; j < len; j++) {
            char t1[50] = "\0", t2[50] = "\0";
            strncpy(t1, str[i], j);         //将单词截断为两部分
            strncpy(t2, str[i] + j, len - j);
            if(Search(t1) && Search(t2)) {
                printf("%s\n", str[i]);
                break;
            }
        }
    }
    Delete(Root);
    return 0;
}





1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值