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

    Hat’s Words

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


    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
     
    Author
    戴帽子的
     
    Recommend
    Ignatius.L
    //字典树、速度够快的、就是太占空间呀
    //判断每个单词是否由给的单词表里的2个单词组成

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #define N 60000
    #define Size 26
    using namespace std;
    struct Tire
    {
      bool isword;
      Tire *next[Size];
    }node [N];
    char s[50000][20];
    int nu;
    void insert(Tire *&root,char *word)
    {
        Tire *p=root;
        while(*word)
        {
            if(p->next[*word-'a']==NULL)
                p->next[*word-'a']=&node[++nu];
             p=p->next[*word-'a'];
            word++;
        }
        p->isword=true;
    }
    bool ask(Tire *root,char *word)
    {
        Tire *p=root;
        while(*word)
        {
            if(p->next[*word-'a']==NULL)
              return false;
            p=p->next[*word-'a'];
          word++;
        }
        if(p->isword) return true;
        return false;
    }
    bool query(Tire *root,char *word)
    {
        Tire *p=root;
        while(*word)
        {
            if(p->next[*word-'a']==NULL)
              return false;
            p=p->next[*word-'a'];
            if(p->isword&&ask(root,word+1))
               return true;
          word++;
        }
        return false;
    }
    int main()
    {
        int n=-1;
        Tire *root=&node[0];
        root->isword=false;
        while(scanf("%s",s[++n])!=EOF)
        {
           insert(root,s[n]);
        }
        for(int i=0;i<=n;i++)
         if(query(root,s[i]))
          printf("%s\n",s[i]);

        return 0;
    }

  • 相关阅读:
    Dobbo介绍
    Java反射
    Rocket MQ简介
    logback异步日志
    Java Arrays.asList
    图解Javascript——作用域、作用域链、闭包
    android源码-安卓源码-Android源码下载-安卓游戏源码
    Ghost版Win8.1系统企业版下载
    3dmax详细讲解全套攻略在线视频教程
    网站分析师如何让分析报告更具价值
  • 原文地址:https://www.cnblogs.com/372465774y/p/2611998.html
Copyright © 2011-2022 走看看