zoukankan      html  css  js  c++  java
  • HDU Hat’s Words

    Hat’s Words

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


    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 <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <cstring>
    #define MAXWORDS 26
    
    char s[50000][50];
    
    typedef struct node
    {
        bool isWord;
        node *next[MAXWORDS];
    }TreeNode;
    
    void ZeroNode(TreeNode *&Node)
    {
        for (int i = 0; i < MAXWORDS; i++)
        {
            Node->next[i] = NULL;
        }
    }
    
    void Insert(TreeNode *&pRoot, char *pstr)
    {
        int nLen = strlen(pstr);
        TreeNode *p = pRoot;
        for (int i = 0; i < nLen; i++)
        {
            int index = pstr[i] - 'a';
            if (p->next[index] == NULL)
            {
                TreeNode *Node = (TreeNode *)malloc(sizeof(TreeNode));
                ZeroNode(Node);
                Node->isWord = false;
                p->next[index] = Node;
            }
            p = p->next[index];
        }
        p->isWord = true;
    }
    
    bool Search(TreeNode *pRoot, char *pstr)
    {
        int nLen = strlen(pstr);
        for (int i = 0; i < nLen; i++)
        {
            int index = pstr[i] - 'a';
            if (pRoot->next[index] != NULL)
            {
                pRoot = pRoot->next[index];
            }
            else
            {
                return false;
            }
        }
        return pRoot->isWord;
    }
    
    void Delete(TreeNode *pRoot)
    {
        for (int i = 0; i < MAXWORDS; i++)
        {
            if (pRoot->next[i] != NULL)
            {
                Delete(pRoot->next[i]);
            }
        }
        free(pRoot);
    }
    
    
    int main()
    {
        int Count = 0;
        TreeNode *pRoot = (TreeNode *)malloc(sizeof(TreeNode));
        ZeroNode(pRoot);
        while(scanf("%s", s[Count]) != EOF)
        {
            Insert(pRoot, s[Count++]);
        }
        for (int i = 0; i < Count; i++)
        {
            int nLen = strlen(s[i]);
            for (int j = 1; j < nLen; j++)
            {
                char temp1[50] = {'\0'};
                char temp2[50] = {'\0'};
                strncpy(temp1, s[i], j);
                strncpy(temp2, s[i] + j, nLen - j);
                if (Search(pRoot, temp1) && Search(pRoot, temp2))
                {
                    printf("%s\n", s[i]);
                    break;
                }
            }
        }
        Delete(pRoot);
        return 0;
    }
  • 相关阅读:
    IDEA中快速排除maven依赖
    Maven构建war项目添加版本号
    运行shell脚本报/bin/bash^M: bad interpreter错误排查方法
    Shell杀tomcat进程
    根据URL下载文件
    关闭Centos的自动更新
    CentOS下建立本地YUM源并自动更新
    为Linux服务器伪装上Windows系统假象
    ServerInfo.INI解密
    请教给终端推销域名的邮件该怎么写?
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3103203.html
Copyright © 2011-2022 走看看