• 紫书第五章训练 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;
    }
  • 相关阅读:
    1015: [JSOI2008]星球大战starwar
    Tyvj P1813 [JSOI2008]海战训练
    1012: [JSOI2008]最大数maxnumber
    1430: 小猴打架
    1270: [BeijingWc2008]雷涛的小猫
    1202: [HNOI2005]狡猾的商人
    1059: [ZJOI2007]矩阵游戏
    3039: 玉蟾宫
    1303: [CQOI2009]中位数图
    1002: [FJOI2007]轮状病毒
  • 原文地址:https://www.cnblogs.com/tzcacm/p/6846225.html
Copyright © 2020-2023  润新知