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. SettingsProvider 它SettingsCache

    转载请注明出处:http://blog.csdn.net/droyon/article/details/35558437 SettingsCache,如指出,SettingsProvider缓冲.这将 ...

  2. NYNU_省赛选拔题(8)

    题目描述 一天萌萌哒孟孟学长去博物馆参观,他想看到更多的东西.博物馆可以表示为N × M细胞的一个矩形区域. “.”表示为路,“*”表示为墙壁,每个墙壁上面都挂有美丽的画卷.孟孟学长可以看到与他所在位 ...

  3. Oracle 用户权限管理方法

    Oracle 用户权限管理方法 sys;//系统管理员,拥有最高权限 system;//本地管理员,次高权限 scott;//普通用户,密码默认为tiger,默认未解锁 sys;//系统管理员,拥有最 ...

  4. 假设动态运行java文字,当在脚本式配置,这是非常方便的

    package com.bfrj.core.groovy; import java.util.HashMap; import java.util.Map; import org.jeecgframew ...

  5. JS编程

    JS编程常识   一.UI层的松耦合 松耦合就是要求各层遵循“最少知识原则”,或者说是各层各司其职,不要越权: HTML:结构层 CSS:表现层 JS:行为层 对于各层的职能,有一句比较贴切的解释:H ...

  6. ajax form表单提交 input file中的文件

    ajax form表单提交 input file中的文件 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为了 ...

  7. C语言学习-数据结构 - 倒插法顺序表

    // test20161106.cpp : Defines the entry point for the console application. // #include "stdafx. ...

  8. JS时间戳比较大小:对于一组时间戳(开始时间~结束时间)和另一组时间戳进行比较,用于判断被比较时间戳组是否在要求范围内

    /* *JS时间戳比较大小:对于一组时间戳(开始时间~结束时间)和另一组时间戳进行比较,用于判断被比较时间戳组是否在要求范围内 *@param date1 date2(形如:'2015-01-01'类 ...

  9. 判断小数点位数不超过2位的JS代码和在删除确认框里面插JS代码

    <script type="text/javascript"> function checkDecimals(){ var decallowed = 2; var re ...

  10. vim打开出现的文档^M什么

    网上公开的一些代码,发现里面多^M符号.这是什么? 我搜索^M没有效果,这应该是一个特殊的控制字符.找换行的结果是不.在每一行的末尾是回车,代替它周围包裹,对于由线定义不同的编码系统是不一样的. li ...