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

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


    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   |   We have carefully selected several similar problems for you:  1075 1671 1298 1800 2846 
     
    给出一个字典里的单词,找出这种单词:由另外两个单词组成。字典树记录所有单词,对于可能的单词进行查询。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #define MAX 600005
    using namespace std;
    int trie[MAX][26];
    bool flag[MAX];
    int pos,num;
    bool may[50000];
    void Insert(char *s,int snum) {
        int i = 0,c = 0;
        while(s[i]) {
            int d = s[i] - 'a';
            if(!trie[c][d]) trie[c][d] = ++ pos;
            c = trie[c][d];
            if(flag[c]) {
                may[snum] = true;
            }
            i ++;
        }
        flag[c] = true;
    }
    bool Query(char *s,int k,int t) {
        if(t == 2) {
            if(!s[k])return true;
            return false;
        }
        int i = k,c = 0;
        bool ok = false;
        while(s[i]) {
            int d = s[i] - 'a';
            if(!trie[c][d]) trie[c][d] = ++ pos;
            c = trie[c][d];
            i ++;
            if(flag[c]) {
                ok |= Query(s,i,t + 1);
            }
        }
        return ok;
    }
    char str[50000][30];
    int main() {
        while(~scanf("%s",str[num])) {
            Insert(str[num],num);
            num ++;
        }
        for(int i = 0;i < num;i ++) {
            if(may[i] && Query(str[i],0,0)) {
                printf("%s
    ",str[i]);
            }
        }
    }
  • 相关阅读:
    Ubuntu 16 安装ElasticSearch
    二叉树
    归并排序
    快速排序
    Git、Github使用
    445. 两数相加 II
    141. 环形链表
    92. 反转链表 II
    19. 删除链表的倒数第N个节点
    2. 两数相加
  • 原文地址:https://www.cnblogs.com/8023spz/p/9628605.html
Copyright © 2011-2022 走看看