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]);
            }
        }
    }
  • 相关阅读:
    SSM应用(五)--参数绑定
    SSM应用(四)--SpringMVC入门
    Oracle常用函数
    SSM应用(三)--Spring整合MyBatis
    SSM应用(二)--注解的使用
    SSM应用(一)--Spring入门
    MyBatis学习(十四)--逆向工程使用
    MyBatis学习(十三)--缓存
    MyBatis学习(十二)--懒加载
    MySQL常用函数
  • 原文地址:https://www.cnblogs.com/8023spz/p/9628605.html
Copyright © 2011-2022 走看看