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;
    }
  • 相关阅读:
    HDU ACM 1071 The area 定积分计算
    Effective C++:条款25:考虑写出一个不抛异常的swap函数
    史上最全站点降权原因解析
    shell脚本中的数学运算
    Spark 1.0.0 横空出世 Spark on Yarn 部署(Hadoop 2.4)
    索尼 LT26I刷机包 X.I.D 增加官方风格 GF A3.9.4 各方面完美
    Swift基础--使用TableViewController自己定义列表
    勒索软件出新招,小心你的隐私和財产安全!
    Http协议具体解释
    Android Studio解决unspecified on project app resolves to an APK archive which is not supported
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3103203.html
Copyright © 2011-2022 走看看