zoukankan      html  css  js  c++  java
  • 紫书第五章训练 uva 10391 Compound Words by crq

    You are to find all the two-word compound words in a dictionary. A two-word compound word is a 
    word in the dictionary that is the concatenation of exactly two other words in the dictionary.

    Input 
    Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will 
    be no more than 120,000 words.

    Output 
    Your output should contain all the compound words, one per line, in alphabetical order.

    Sample Input 

    alien 
    born 
    less 
    lien 
    never 
    nevertheless 
    new 
    newborn 
    the 
    zebra

    Sample Output 
    alien 
    newborn

    题意分析:给定一系列单词(总共不超过120000个单词),找出某些单词,它们是由其他两个单词组合而成的,按照字典序排列。因为所有的单词已经按照字典序排好了,因此从前往后逐个判断即可,因为单词的长度没有给出来,但估计最大长度len不会太大记为常量,将单词拆分成两个单词,查找是否存在于列表中即可。查找直接通过map(或二分查找),总计复杂度约O(N*logN*LEN*LEN)

    AC源码:

    #include <stdio.h>
    #include <string>
    #include <map>
    using namespace std;
    
    int main()
    {
    //    freopen("d:\data1.in","r",stdin);
        map<string, int> mp;
        char s[100];
        while(scanf("%s", s)!=EOF)
        {
            mp[s] = 1;
        }
        map<string, int>::iterator it;
        for(it=mp.begin();it!=mp.end();it++)
        {
            for(int i=1;i<it->first.length()-1;i++)
            {
                string ss1 = it->first.substr(0, i);
                string ss2 = it->first.substr(i, it->first.length()-i);
                if(mp.find(ss1)!=mp.end() && mp.find(ss2)!=mp.end())
                {
                    printf("%s
    ", it->first.c_str());
                    break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    最近的题越来越难了,卧槽,搞一上午一题不会,题解也看不懂
    hdu 4630 树状数组 ****
    hdu 3473 划分树 ***
    hdu 3360 最小点覆盖 **
    hdu 1507 记录路径的二分匹配 **
    poj 3177 边双联通 **
    hdu 4612 边双联通 ***
    Elasticsearch聚合后分页深入详解
    redis 五种数据类型及其使用场景
    再有人问你分布式锁,这篇文章扔给他
  • 原文地址:https://www.cnblogs.com/tzcacm/p/6846225.html
Copyright © 2011-2022 走看看