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;
    }
  • 相关阅读:
    java Boolean和boolean的区别
    stack.isEmpty()和empty()
    mysql中文、英文别名排序问题,order by 关键字详解
    数组中array==null和array.length==0的区别
    7.9总结
    7.8总结
    6.25总结
    6.27总结
    6.29总结
    6.28总结
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3103203.html
Copyright © 2011-2022 走看看