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;
    }
  • 相关阅读:
    Nightingale 加发邮件功能
    Centos7 系统进程解析
    Nightingale 夜莺监控系统 安装
    iptables:应用防火墙规则:ptables-restore: line xxx failed [失败]
    CAD2011卸载不干净 无法重装问题
    Make Globle HS 全局热备
    Swarm工具使用
    docker-registry构建私有的镜像仓库
    MySQL Replication
    javascript 设计模式之单例模式
  • 原文地址:https://www.cnblogs.com/tzcacm/p/6846225.html
Copyright © 2011-2022 走看看