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]);
            }
        }
    }
  • 相关阅读:
    JavaScript实现html购物车代码
    SQLServer2019安装教程
    SQLServer2019安装教程
    pragma指令简介
    16进制字符串转数字(C/C++,VB/VB.net,C#)
    对方网络非正常断开检测方法
    stl map高效遍历删除的方法
    memmove 和 memcpy的区别
    Socket 心跳包机制总结
    如何将内存中的位图数据绘制在DC上
  • 原文地址:https://www.cnblogs.com/8023spz/p/9628605.html
Copyright © 2011-2022 走看看