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;
    }
  • 相关阅读:
    centos7 rabbitmq系统部署
    socket粘包、断包、校验
    C#对象、文件与二进制串(byte数组)之间的转换
    Windows Error Code
    C#之Socket断线和重连
    BitConverter 整数和十六进制互转
    DateTime还是DateTimeOffset?Now还是UtcNow?
    WebAPI 跨域
    Console Owin 跨域解决
    2019.12.17 Arcgis10.1许可到期解决方法
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3103203.html
Copyright © 2011-2022 走看看