• 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;
    }
  • 相关阅读:
    Bootstrap(6)图标菜单按钮组件
    Bootstrap(6)辅组类和响应式工具
    Bootstrap(5)栅格系统
    Bootstrap(4) 表单和图片
    Bootstrap(3) 表格与按钮
    Bootstrap(2) 排版样式
    Bootstrap(1) 概述与环境搭建
    requirejs简单应用
    Validate常用校验
    VSS2005源代码管理启用http方式
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3103203.html
Copyright © 2020-2023  润新知