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;
    }
  • 相关阅读:
    c# 基本值类型及其默认值
    军史馆如何营造更好的意境
    部队营区生态环境设计与文化氛围营造
    我的第一个CAD程序
    经典SQL语句大全
    带图标和多行显示的ListBox
    CAD 二次开发 -- 自动加载开发的DLL
    Git使用总结
    OpenStack迁移虚拟机流程分析
    OpenStack创建虚拟机流程
  • 原文地址:https://www.cnblogs.com/tzcacm/p/6846225.html
Copyright © 2011-2022 走看看