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]);
            }
        }
    }
  • 相关阅读:
    Qt:QT右键菜单
    Ubuntu 12.04安装字体
    在ubuntu下关闭笔记本触摸板
    SSL与TLS的区别以及介绍
    隔行扫描与逐行扫描的区别
    Linux下的绘图(流程图、UML、mindmap)工具
    RedHat Linux 下安装MPlayer 编译源代码方式
    循环数组实现队列的四种方式
    Linux内核spin_lock与spin_lock_irq分析
    Oracle 创建同义词
  • 原文地址:https://www.cnblogs.com/8023spz/p/9628605.html
Copyright © 2011-2022 走看看