UVA 10391 Compound Words
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的更多相关文章
- uva 10391 Compound Words <set>
Compound Words You are to find all the two-word compound words in a dictionary. A two-word compound ...
- UVA 10391 - Compound Words 字符串hash
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 复合词(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 ...
- 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 ...
- UVa 10391 (水题 STL) Compound Words
今天下午略感无聊啊,切点水题打发打发时间,=_=|| 把所有字符串插入到一个set中去,然后对于每个字符串S,枚举所有可能的拆分组合S = A + B,看看A和B是否都在set中,是的话说明S就是一个 ...
- 【UVA】10391 Compound Words(STL map)
题目 题目 分析 自认已经很简洁了,虽说牺牲了一些效率 代码 #include <bits/stdc++.h> using namespace std; set <s ...
- UVA 10391 stl
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 10391
这个题,单纯做出来有很多种方法,但是时间限制3000ms,因此被TL了不知道多少次,关键还是找对最优解决方法,代码附上: #include<bits/stdc++.h> using nam ...
- 复合词 (Compund Word,UVa 10391)
题目描述: 题目思路: 用map保存所有单词赋键值1,拆分单词,用map检查是否都为1,即为复合词 #include <iostream> #include <string> ...
随机推荐
- ftk学习记录(脚本文章)
[声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 上一篇说到了对话框.今天就看看结果. watermark/2/text/aHR0cDovL2Js ...
- 从头开始学JavaScript (十)——垃圾收集
原文:从头开始学JavaScript (十)--垃圾收集 一.垃圾收集 1.1javascript垃圾收集机制: 自动垃圾收集,执行环境会负责管理代码执行过程中的使用的内存.而在C和C++之类的语言中 ...
- sicily 1007 To and Fro (基地称号)
链接:http://soj.me/show_problem.php?pid=1007 Description Mo and Larry have devised a way of encrypting ...
- android ndk通过遍历和删除文件
在做移动开发过程,难免有些本地文件管理操作.例如,很常见app随着微博.微信要清除缓存功能,此功能是走app文件夹.然后删除所有缓存文件.使用java的File类能够实现本地文件遍历及删 ...
- (转)javabean操作文件正确,但是Jsp调用javabean时文件路径出错问题解决之JavaBean访问本地文件实现路径无关实现方法
在JSP中,页面链接是使用web路径的,但如果JavaBean要访问本地文件读取配置信息的话,是需要文件的本地路径的.如果你在写 Bean的时候直接将本地路径写进去,那网站的路径就不能变化,丧 ...
- 解决Crystal Report XI R2不能在64操作系统正常工作的问题-web程序
原文:[原创]解决Crystal Report XI R2不能在64操作系统正常工作的问题-web程序 我更换了新的电脑,操作系统也从原来32位的windows 2003 R2升级到windows 2 ...
- 基于.NET MVC的高性能IOC插件化架构
基于.NET MVC的高性能IOC插件化架构 最近闲下来,整理了下最近写的代码,先写写架构,后面再分享几个我自己写的插件 最近经过反复对比,IOC框架选择了Autofac,原因很简单,性能出众,这篇博 ...
- CSS3+HTML5特效1 - 上下滑动效果
先看看效果,把鼠标移上去看看. back front 1. 本实例需要以下元素: a. 外容器 box b. 内容器 border c. 默认显示内容 front d. 滑动内容 back 2. 外容 ...
- iOS_22自定义键盘工具栏
最后效果图: Main.storyboard KeyboardTool.xib watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ ...
- easyui datagrid 单元格编辑 即见即所得,MVC菜单维护,扫描增加
效果如图: 参见 EasyUI 官方 Demo 及文档 @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Lay ...