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]);
            }
        }
    }
  • 相关阅读:
    SQLAlchemy使用说明之ORM
    Python日志模块logging
    Python正则表达式模块re
    group by 小结
    Hive Beeline 官方文档学习
    在MySQL中创建cm-hive使用的数据库及账号
    MySQL导入数据错误error: 13 及解决办法
    Yum 安装并设置 MySQL
    使用xshell5 从CentOS主机download资料
    Bootstrap 学习
  • 原文地址:https://www.cnblogs.com/8023spz/p/9628605.html
Copyright © 2011-2022 走看看