Problem E: 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

a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra

Sample Output

alien
newborn

题意:输入一些单词。要输出其中,可以由两个单词拼接组合成的单词。。

思路:题中数据有12W,,直接暴力时间复杂度为肯定会超时。。需要高效的方法

我是用map,输入的时候把每个单词标记成1。然后每个单词找过去。每个单词从每个字母位置分成2个单词。如果2个单词均是有标记成1的,代表这个单词可以被组合,就输出来

这样做的话。时间复杂度接近于O(n)。。。A之。。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <map>
using namespace std; char ci[122222][25];
int len;
map<string, int> adj;
int main()
{
int n = 0;
while (gets(ci[n]) != NULL)
{
adj[ci[n]] = 1;
n ++;
}
for (int i = 0; i < n ; i ++)
{
len = strlen(ci[i]);
for (int j = 1; j < len - 1; j ++)
{
char a[25], b[25];
for (int k = 0; k < j; k ++)
a[k] = ci[i][k];
a[j] = '\0';
for (int k = j; k < len; k ++)
b[k - j] = ci[i][k];
b[len - j] = '\0';
if (adj[a] == 1 && adj[b] == 1)
{
printf("%s\n", ci[i]);
break;
}
}
}
return 0;
}

UVA 10391 Compound Words的更多相关文章

  1. uva 10391 Compound Words <set>

    Compound Words You are to find all the two-word compound words in a dictionary. A two-word compound ...

  2. UVA 10391 - Compound Words 字符串hash

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  3. 复合词(Compound Words, UVa 10391)(stl set)

    You are to find all the two-word compound words in a dictionary. A two-word compound word is a word i ...

  4. Compound Words UVA - 10391

      You are to find all the two-word compound words in a dictionary. A two-word compound word is a wor ...

  5. UVa 10391 (水题 STL) Compound Words

    今天下午略感无聊啊,切点水题打发打发时间,=_=|| 把所有字符串插入到一个set中去,然后对于每个字符串S,枚举所有可能的拆分组合S = A + B,看看A和B是否都在set中,是的话说明S就是一个 ...

  6. 【UVA】10391 Compound Words(STL map)

    题目 题目     分析 自认已经很简洁了,虽说牺牲了一些效率     代码 #include <bits/stdc++.h> using namespace std; set <s ...

  7. UVA 10391 stl

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. uva 10391

    这个题,单纯做出来有很多种方法,但是时间限制3000ms,因此被TL了不知道多少次,关键还是找对最优解决方法,代码附上: #include<bits/stdc++.h> using nam ...

  9. 复合词 (Compund Word,UVa 10391)

    题目描述: 题目思路: 用map保存所有单词赋键值1,拆分单词,用map检查是否都为1,即为复合词 #include <iostream> #include <string> ...

随机推荐

  1. asp.net学习之扩展GridView

    原文:asp.net学习之扩展GridView 本节讨论如何从现有的控件,进而扩展成强大的,更定制的GridView控件 1.扩展BoundField 默认的BoundField不能显示多文本,文字一 ...

  2. WinForm实现类似QQ停靠,显示隐藏过程添加特效效果

    原文:WinForm实现类似QQ停靠,显示隐藏过程添加特效效果 这可能是个老题长谈的问题了,只是在项目中会用到这个效果,所以今天做个记录.大家见了别喷我.在项目中的需求是这样的. 打开程序,在屏幕的右 ...

  3. DevExpress XtraReports 入门一 创建 Hello World 报表

    原文:DevExpress XtraReports 入门一 创建 Hello World 报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助更 ...

  4. 文《左右c++与java中国的垃圾问题的分析与解决》一bug分析

    文<左右c++与java中国的垃圾问题的分析与解决>一bug分析 DionysosLai(906391500@qq.com) 2014/10/21 在前几篇一博客<关于c++与jav ...

  5. BNUOJ 34981 A Matrix

    BNUOJ 34981 A Matrix 题目地址:pid=34981" style="color:rgb(0,136,204); text-decoration:none&quo ...

  6. Controller与Action

    Controller与Action 我们知道在MVC5和之前的版本,两个框架的生命周期是不一样的,在新版MVC6中,MVC Controller/Web API Controller已经合二为一了,本 ...

  7. 经典算法题每日演练——第八题 AC自动机

    原文:经典算法题每日演练--第八题 AC自动机 上一篇我们说了单模式匹配算法KMP,现在我们有需求了,我要检查一篇文章中是否有某些敏感词,这其实就是多模式匹配的问题. 当然你也可以用KMP算法求出,那 ...

  8. 【iOS发展-70】点菜系统案例:使用文本框inputView和inputAccessoryView串联UIPickerView、UIDatePicker和UIToolBar

    (1)效果 (2)先在storyboard中设计界面,然后源码(直接在ViewController中码) #import "ViewController.h" @interface ...

  9. oracle substr功能

    substr(string dealstr, int startposition, int sublength) dealstr:截取字符串 startposition:串dealstr,起始位置0 ...

  10. java_eclipse_maven_svn_主题彩色插件_全屏插件

    作为一名不算新手的猿猿,还来纠结IDE环境搭建实属不该,不过着实纠结了不少时间. target: eclipse + maven +svn + 设置默认编码+全屏 绕的路就不说了,直奔主题,由于mav ...