紫书第五章训练2 F - Compound Words

F - Compound Words

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

这个题就是找个单词是已出现两个单词的和,我用map写一直错, 我分析了下大概是每次都要访问map,然后map的空间就不够了,所以还是用set搞下不错,他和map的查询复杂度都是logn的,运用string还有find岂不是很简单实用,美滋滋

不过需要提出的是字典树或者直接数组搞会比stl速度快些的

#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
set<string>ma;
while(cin>>s){
    ma.insert(s);
}
set<string>::iterator it;
for(it=ma.begin();it!=ma.end();it++){
    string c=*it;
    for(int i=1;c[i];i++){
        if(ma.find(c.substr(0,i))!=ma.end()&&ma.find(c.substr(i))!=ma.end()){
            cout<<c<<endl;
            break;
        }
    }
}

return 0;}

时间: 2024-10-09 00:57:35

紫书第五章训练2 F - Compound Words的相关文章

紫书第五章训练 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

紫书第五章训练3 D - Throwing cards away I

D - Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move

紫书第五章训练 uva 10763 Foreign Exchange by crq

Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task.The

紫书第五章训练 uva 1594 Ducci Sequence by crq

Description A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers: ( a1, a2, ... , an)  (| a1 - a

紫书第三章训练2 暴力集

A - Master-Mind Hints MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the leng

lrj紫书第五章

UVA-1592 1 // UVa1592 Database 2 // Rujia Liu 3 // 本程序只是为了演示STL各种用法,效率较低.实践中一般用C字符串和哈希表来实现. 4 5 #include<iostream> 6 #include<cstdio> 7 #include<vector> 8 #include<string> 9 #include<map> 10 #include<sstream> 11 using n

紫书第四章训练 UVA1589 Xiangqi by 15 周泽玺

Xiangqi is one of the most popular two-player board games in China. The game represents a battle between two armies with the goal of capturing the enemy's "general" piece. In this problem, you are given a situation of later stage in the game. Be

紫书第4章 函数和递归

1  序 系统的整理下第四章的学习笔记.同上次一样,尽量在不依赖书本的情况下自己先把例题做出来.这次有许多道题代码量都比较大,在例题中我都用纯C语言编写,但由于习题的挑战性和复杂度,我最终还是决定在第五章开始前,就用C++来完成习题.不过所有的代码都是能在C++提交下AC的. 在习题中,我都习惯性的构造一个类来求解问题,从我个人角度讲,这会让我的思路清晰不少,希望自己的一些代码风格不会影响读者对解题思路的理解. 其实在第四章前,我就顾虑着是不是真的打算把题目全做了,这些题目代码量这么大,要耗费很

紫书第三章 数组和字符串

1  序 系统的整理下第三章的学习笔记.例题代码是在未看书本方法前自己尝试并AC的代码,不一定比书上的标程好:习题除了3-8百度了求解方法,其它均独立完成后,会适当查阅网上资料进行整理总结.希望本博文方便自己日后复习的同时,也能给他人带来点有益的帮助(建议配合紫书--<算法竞赛入门经典(第2版)>阅读本博客).有不足或错误之处,欢迎读者指出. 2  例题 2.1  UVa272--Tex Quotes #include <stdio.h> int main() { bool log