zoukankan      html  css  js  c++  java
  • HDU

    HDU - 1247
    Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

     Status

    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: 2486
    Memory: 9096 KB		Time: 31 MS
    Language: G++		Result: Accepted
    */
    //这道题目思路非常easy
    //就是找两个单词组成还有一个单词就可
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <iostream>
    #include <queue>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int MAXN = 1000000 + 5;
    const int MAXM = 50000 + 5;
    const int INF = 0x3f3f3f3f;
    struct node {
        int v,next[30];
        void init() {
            v = -1;
            memset(next, -1, sizeof(next));
        }
    } L[MAXN];
    
    int tot;
    char str[MAXM][100];
    priority_queue<string , vector<string>, greater<string> >P;
    
    void add(char * a, int len) {
        int now = 0;
        for(int i = 0; i < len ; i ++) {
            int tmp = a[i] - 'a';
            int next = L[now].next[tmp];
            if(next == -1) {
                next  = ++ tot;
                L[next].init();
                L[now].next[tmp] = next;
            }
            now = next;
        }
        L[now].v = 0;
    }
    
    bool querys(char * a) {
        int len = strlen(a);
        int now  = 0;
        for(int i = 0; i < len; i ++) {
            int tmp = a[i] - 'a';
            int next = L[now].next[tmp];
            if(next == -1) return false;
            now = next;
        }
        return L[now].v == 0;
    }
    
    bool query(char * a, int len) {
        int now = 0;
        for(int i = 0; i < len; i ++) {
            int tmp = a[i] - 'a';
            int next = L[now].next[tmp];
            if(L[now].v == 0) {
                if(querys(a + i)) return true;//是否存在还有一个单词能够组成剩下的字符串
            }
            now = next;
        }
        return false;
    }
    
    int main() {
        int cnt = 0, Min = INF;
        L[0].init();
        tot = 0;
        //freopen("D://imput.txt", "r", stdin);
        while(~scanf("%s", str[cnt ++])) {
            add(str[cnt - 1], strlen(str[cnt - 1]));
            Min = min(Min, int(strlen(str[cnt - 1])));
        }
        for(int i = 0; i < cnt ; i ++) {
            if(Min * 2 > strlen(str[i]))continue;
            if(query(str[i], strlen(str[i]))) P.push(str[i]);
        }
        string s;
        while(!P.empty()) {//优先队列自己主动排序也可使用set或者map
            s = P.top();
            P.pop();
            cout<<s<<endl;
        }
        return 0;
    }

  • 相关阅读:
    Session_End引发的性能问题!
    可能引发性能问题的几个写法,看看你占哪一个.
    优化你的DiscuzNT3.0,让它跑起来(2)发帖回帖篇
    什么是经济学
    生产可能性边界和机会成本
    九宫格的实现(转)
    LAMP的安装和配置
    iPhone 开发过程中的一些小技术的总结(转)
    有效的利用资源边际成本与边际利益
    Dijkstra算法(注:单源最短路径的贪心算法)和数学归纳法<转>
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7262394.html
Copyright © 2011-2022 走看看