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

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
const int maxn=2e5;
int a[maxn], t = -1, sum = 0;
char s[maxn][30], ch1[30], ch2[30]; int Hash(char *str)
{
int seed = 31, hash = 0;
while (*str)
hash = hash * seed + *str++;
return hash & 0x7FFFFFFF;
}
int Find(int key, int l, int r)
{
int mid = (l + r) / 2;
if(l>r) return 0;
if(a[mid]==key) return 1;
else if(a[mid]>key) Find(key, l, mid - 1);
else Find(key, mid + 1, r);
}
int main()
{
int i, j, k, l;
while(gets(s[++t])) a[t] = Hash(s[t]);
sort(a, a + t);
for (i = 0; i<t; i++)
{
l = strlen(s[i]);
for (j = 0; j<l - 1; j++)
{
for (k = 0; k <= j; k++)
ch1[k] = s[i][k];
ch1[j + 1] = '\0';
for (k = j + 1; k<l; k++)
ch2[k - j - 1] = s[i][k];
ch2[l - j - 1] = '\0';
if (Find(Hash(ch1), 0, t - 1) + Find(Hash(ch2), 0, t - 1) == 2)
{
printf("%s\n",s[i]);
break;
}
}
}
return 0;
}

UVA-10391 Compoud Words的更多相关文章

  1. UVA 10391 stl

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

  2. UVA 10391 - Compound Words 字符串hash

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

  3. UVa 10391 (水题 STL) Compound Words

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

  4. uva 10391 Compound Words <set>

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

  5. UVA 10391 Compound Words

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

  6. uva 10391

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

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

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

  8. 复合词(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 ...

  9. 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 ...

  10. UVa第五章STL应用 习题((解题报告))具体!

    例题5--9 数据库 Database UVa 1592 #include<iostream> #include<stdio.h> #include<string.h&g ...

随机推荐

  1. 【集合系列】- 深入浅出分析Collection中的List接口

    一.List简介 List 的数据结构就是一个序列,存储内容时直接在内存中开辟一块连续的空间,然后将空间地址与索引对应. 以下是List集合简易架构图 由图中的继承关系,可以知道,ArrayList. ...

  2. python 基础之 模块

    Python 基础之模块 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 就是一个python文件中定义好了类和方法,实现了一些功能,可以被别的python文 ...

  3. [LC]783题 二叉搜索树结点最小距离(中序遍历)

    ①题目 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null]输出: 1解释:注意,root是树结点对象(T ...

  4. Arduino 基于 ESP8266 配置WIFI模块

    Arduino 基于 ESP8266 配置WIFI模块 使用ESP8266作为服务器,使用浏览器访问该服务器,从而控制LED灯 选择 [文件]->[示例]->[ESP8266WIFI]-& ...

  5. 0x8000FFFF 错误的解决方式

    问题描述: 在F盘新建文件夹或文件的时候提示0x8000FFFF灾难性错误: 解决方法: 1.在F盘的位置,右击选择属性 2.在弹出的窗口中选择工具,点击检查 3.根据系统提示进行响应的驱动扫描与修复 ...

  6. Python3.7.1学习(五) 将列表中的元素转化为数字并排序

    # 本文实例讲述了Python中列表元素转为数字的方法.分享给大家供大家参考,具体如下: # 有一个数字字符的列表: numbers = ['2', '4', '1', '3']print(numbe ...

  7. JVM浅谈

    **前言** 由于先前也遇到过一些性能问题,OOM算是其中的一大类了.因此也对jvm产生了一些兴趣.自己对jvm略做了些研究.后续继续补充. **从oom引申出去** 既然说到oom,首先需要知道oo ...

  8. windows版本 MongoDB副本集搭建及开启身份验证

    ------------恢复内容开始------------ ------------恢复内容开始------------ MongoDB副本集搭建 我搭建的是一个主节点,两个副节点 构建目录结构如下 ...

  9. libwebsocket协议切换状态机

    libwebsocket为连接(connection)定义了一组状态机-lws_connection_states,通过状态机我们来看libwebsocket如何实现协议的切换.除了lws_conne ...

  10. HashSet源码学习,基于HashMap实现

    HashSet源码学习 一).Set集合的主要使用类 1). HashSet 基于对HashMap的封装 2). LinkedHashSet 基于对LinkedHashSet的封装 3). TreeS ...