Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7502    Accepted Submission(s): 2705
Problem Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.

You are to find all the hat’s words in a dictionary.
 
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.

Only one case.
 
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
 
Sample Input
a
ahat
hat
hatword
hziee
word
 
Sample Output
ahat
hatword

这题開始总想着走捷径,想依据trie树内部节点将单词分成前后缀,前缀一定在树里,所以仅仅需推断后缀是否在树里,可是非常多细节非常棘手,比方如何确定后缀。后缀如何检索。如何在一个单词内改变前后缀等,折腾了非常久,实在没法解决,然后就用了一開始就非常鄙夷的方法,将每一个字符串都存到数组并插入到树里。然后将每一个字符串遍历拆分成前后缀,再检索前后缀是否都在树中。

这题再次验证了一个道理,就是在没有想出更好的方法之前。最笨的方法就是最好的方法。

#include <stdio.h>
#include <string.h>
#include <stdlib.h> struct Node{
struct Node *next[26];
int wordCover;
};
Node *root = (Node *)malloc(sizeof(Node));
char suffix[50], prefix[50], strArr[50000][50]; void cleanStruct(Node *p)
{
memset(p->next, 0, sizeof(p->next));
p->wordCover = 0;
} void insert(char *str)
{
int id;
Node *p = root;
while(*str)
{
id = *str - 'a';
if(p->next[id] == NULL){
p->next[id] = (Node *)malloc(sizeof(Node));
cleanStruct(p->next[id]);
}
p = p->next[id];
++str;
}
++p->wordCover;
} int isExist(char *str)
{
Node *p = root;
int id;
while(*str){
id = *str - 'a';
if(p->next[id] == NULL) return 0;
p = p->next[id];
++str;
}
return p->wordCover;
} void deleteTrie(Node *p)
{
for(int i = 0; i < 26; ++i)
if(p->next[i]) deleteTrie(p->next[i]);
free(p);
} int main()
{
//freopen("stdin.txt", "r", stdin);
int id = 0, i, j, len;
cleanStruct(root);
while(gets(strArr[id])) insert(strArr[id++]);
for(i = 0; i < id; ++i){
len = strlen(strArr[i]);
for(j = 1; j < len; ++j){
strcpy(prefix, strArr[i]);
prefix[j] = '\0';
strcpy(suffix, strArr[i] + j);
if(isExist(prefix) && isExist(suffix)){
puts(strArr[i]); break;
}
}
}
deleteTrie(root);
return 0;
}

2014.12.16更新

#include <stdio.h>
#include <string.h> #define maxn 1000000 char str[50], str1[50], str2[50], dic[50002][50];
struct Trie {
int ch[maxn][26];
int val[maxn], sz; Trie() {
memset(ch[0], 0, sizeof(ch[0]));
sz = 1;
}
int idx(char ch) { return ch - 'a'; };
void insert(const char *str) {
int u = 0, i, id, len = strlen(str);
for (i = 0; i < len; ++i) {
id = idx(str[i]);
if (!ch[u][id]) {
memset(ch[sz], 0, sizeof(ch[sz]));
ch[u][id] = sz;
val[sz++] = 0;
}
u = ch[u][id];
}
val[u] = 1;
}
bool find(const char *str) {
int u = 0, i, id, len = strlen(str);
for (i = 0; i < len; ++i) {
id = idx(str[i]);
if(!ch[u][id]) return false;
u = ch[u][id];
}
return val[u];
}
} T; int main() {
// freopen("stdin.txt", "r", stdin);
int id = 0, i, j, len;
while (scanf("%s", str) == 1) {
T.insert(str);
strcpy(dic[id++], str);
}
for (i = 0; i < id; ++i) {
len = strlen(dic[i]);
for (j = 1; j < len; ++j) {
strncpy(str1, dic[i], j);
strncpy(str2, dic[i] + j, len - j);
str1[j] = '\0';
str2[len-j] = '\0';
if (T.find(str1) && T.find(str2)) {
puts(dic[i]); break;
}
}
}
return 0;
}

HDU1247 Hat’s Words 【trie树】的更多相关文章

  1. HDU1247 - Hat’s Words(Trie树)

    题目大意 给定一些单词,要求你把所有的帽子单词找出来,如果某个单词恰好由另外两个单词连接而成,那么它就是帽子单词 题解 先把所有单词插入到Trie树,然后判断每个单词是不是帽子单词,做法就是:对于第i ...

  2. Hdu 1247 Hat's Words(Trie树)

    Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...

  3. hdu-1247 Hat’s Words---字典树模板

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1247 题目大意: 给出一些单词,以EOF结束,看其中哪一个单词可以由其他两个单词组成,将其输出 解题 ...

  4. DHU--1247 Hat’s Words && HiHocder--1014 Trie树 (字典树模版题)

    题目链接 DHU--1247 Hat’s Words HiHocder--1014 Trie树 两个一个递归方式一个非递归 HiHocoder #include<bits/stdc++.h> ...

  5. hdu1247 Hat’s Words

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1247 题目: Hat's Words Time Limit: 2000/1000 MS (Ja ...

  6. Trie树入门及训练

    什么叫Trie树? Trie树即字典树. 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本 ...

  7. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  8. 基于trie树的具有联想功能的文本编辑器

    之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...

  9. hihocoder-1014 Trie树

    hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...

随机推荐

  1. 数据逆向传递 unwind segue

    一.简介 unwind segue通过允许你定义一个控制器和其他控制器的关系来扩展segue的概念,这个“关系”先于顺传(流式控制)的方式.基于unwind segue可以实现导航相反的效果,即将界面 ...

  2. CSS样式的优势

    为什么使用css样式来设置网页的外观样式呢?下面是一段文字,我们想把“超酷的互联网”.“服务及时贴心”.“有趣易学”这三个短语的文本颜色设置为红色,这时就 可以通过设置样式来设置,而且只需要编写一条c ...

  3. 基于GBT28181:SIP协议组件开发-----------第一篇环境搭建

    原创文章,引用请保证原文完整性,尊重作者劳动,原文地址http://www.cnblogs.com/qq1269122125/p/3930018.html,qq:1269122125. SIP协议在安 ...

  4. 使用C#连接ORACLE数据库

    一.使用OracleClient组件连接Oracle   .Net框架的System.Data.OracleClient.dll组件(ADO.Net组件),为连接和使用Oracle数据库提供了很大的方 ...

  5. TCP/IP-IP

    A contented mind is a perpetual feast. "知足长乐" 参考资料:TCP/IP入门经典 (第五版) TCP/IP详解 卷一:协议 一.简介 IP ...

  6. 【转载】Think as Customer 以客户为中心的测试理念

    纵观各大公司的核心理念,往往都有一条类似“以客户为中心”的价值观.华为公司更是把“以客户为中心”放在其核心价值观的第一条,以显示它的重要性.从我 们入职培训开始,公司就反复强调并引导大家深入讨论,希望 ...

  7. Spring4.0学习笔记(9) —— Spring泛型依赖注入

    1.定义基础仓库 package com.spring.generic.di; public class BaseRepository<T> { } 2.定义基础服务层 package c ...

  8. Uncaught SyntaxError: Unexpected end of input

    js报错  原因:输入的意外终止…… 页面代码写的不规范啊……其中的某条语句,没有正常结束…… 或者部分语句“‘’”双引号,单引号没有配对好,被转义了之类的……错误造成的 代码: <script ...

  9. 关于 wait_event_interruptible() 和 wake_up()的使用

    来源:http://blog.csdn.net/allen6268198/article/details/8112551 1. 关于 wait_event_interruptible() 和 wake ...

  10. Sqlserver 正则替换函数的一种实现

    --函数 IF OBJECT_ID(N'dbo.RegexReplace') IS NOT NULL DROP FUNCTION dbo.RegexReplace GO CREATE FUNCTION ...