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> ...
随机推荐
- DevExpress XtraReports 入门一 创建 Hello World 报表
原文:DevExpress XtraReports 入门一 创建 Hello World 报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助更 ...
- Codeforces Round #266 (Div. 2)-C,D
C - Number of Ways 直接暴力从前往后寻找.假设找到1/3sum的位置,那么标记++.找到2/3的位置,总数加上标记数. #include<stdio.h> #includ ...
- this、访问修饰符——Java笔记(五)
this 表示当前对象 谁调用方法谁就是当前对象 用static修饰的代码块里面不能使用this 方法里面有一个和字段同名的局部变量时,不能省略this this还 ...
- 使用Blend的一些问题
原文:使用Blend的一些问题 WPF开发,界面处理首选Blend,如果你开发了两年WPF都没接触过blend(当然这种几率不高),或者你刚接触WPF,可以考虑使用Blend,这货也算得上一个神器,上 ...
- Leetcode:unique_binary_search_trees
一. 称号 给定的数目n.问:有多少种不同BST(二叉搜索树) 比如: 因为N =3,共同拥有5种独特的BST. 1 3 3 2 1 \ ...
- 导入三方包,出现ClassNotFoundException
在项目中须要引用settings模块里面的某个活动.在eclipse中导入settins.jar包之后,使用例如以下方式启动: Intent intent = new Intent(); intent ...
- 从头开始学JavaScript (五)——操作符(二)
原文:从头开始学JavaScript (五)--操作符(二) 一.乘性操作符 1.乘法:* 乘法操作符的一些特殊规则: 如果操作数都是数值,按照常规的乘法计算,如果乘积超过了ECMAscri ...
- 位记录——Windows 7已安装Sublime Text 3、cynwin、SublimeClang
转载请注明出处:http://blog.csdn.net/cywosp/article/details/34429697 1. 到https://www.cygwin.com/下载setup-x86_ ...
- 泛型方法动态生成表达式树 Expression
public string GetGridJSON(TraderInfo model) { IQueryable<TraderInfo> Temp = db.TraderInfo; if ...
- cocos2d-x 移植android竖,横屏设置
AndroidManifest.xml于android:screenOrientation现场控制屏幕方向,默认为横屏 android:screenOrientation="landscap ...