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. js获取文件里面的所有文件名

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 面试总问的jvm调优到底是要干什么?

    1. 压力测试的理解,xxx的性能10w/s,对你有意义么? 没有那家卖瓜的会说自己家的不甜,同样,没有哪个开源项目愿意告诉你在对它条件最苛刻的时候压力情况是多少,一般官网号称给你看的性能指标都是在最 ...

  3. count的一些用法

    count(*)包括了所有的列,相当于行数,在统计结果的时候,不会忽略列值为NULL  count(1)包括了所有列,用1代表代码行,在统计结果的时候,不会忽略列值为NULL  count(列名)只包 ...

  4. mysql 创建用户及授权(1)

    一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用户名 host:指定该用户 ...

  5. Blob字段

    在oracle中有一个特殊的字段类型Blob,Blob是指二进制大对象也就是英文Binary Large Object的缩写: 通过sql取出后,需要进行特殊处理.而我是这样玩的: 1.通过sql查询 ...

  6. Linux菜鸟——常见命令一 权限

    Linux对文件和目录的权限位 权限位是十位 第一位 代表文件类型 - 普通文件 d 目录文件 l 链接文件 后面九尾 所有者权限 u = user 所属组权限 g = group 其他人权限 o = ...

  7. [apue] 神奇的 Solaris pipe

    说到 pipe 大家可能都不陌生,经典的pipe调用配合fork进行父子进程通讯,简直就是Unix程序的标配. 然而Solaris上的pipe却和Solaris一样是个奇葩(虽然Solaris前途黯淡 ...

  8. Android 获取 SHA1值3步完成

    未经允许,禁止

  9. JSON的使用场景及注意事项介绍

    上篇我们讲解了JSON的诞生原因是因为XML整合到HTML中各个浏览器实现的细节不尽相同,所以道格拉斯·克罗克福特(Douglas Crockford) 和 奇普·莫宁斯达(Chip Mornings ...

  10. Lab6:进程的调度

    CPU调度 从就绪队列中挑选下一个占用CPU运行的进程,从多个可用CPU中挑选就绪进程可使用的CPU资源 调度策略 比较调度算法的准则 CPU使用率 吞吐量 周转时间 就绪等待时间 响应时间 吞吐量与 ...