HDU 1247 Hat’s Words
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13752 Accepted Submission(s): 4919
You are to find all the hat’s words in a dictionary.
Only one case.
#include <cstdio>
#include <cstring> char s[50005][50]; struct Node{
Node* pt[26];
bool isEnd;
}; Node* root;
Node memory[100000];
int allo; void trie_init()
{
memset(memory, 0, sizeof(memory));
allo = 0;
root = &memory[allo++];
} void trie_insert(char str[])
{
Node *p = root;
for(int i = 0; str[i] != '\0'; ++i){
int id = str[i]-'a';
if(p->pt[id] == NULL){
p->pt[id] = &memory[allo++];
}
p = p->pt[id];
}
p->isEnd = true;
} bool trie_find(char str[])
{
Node *p = root;
for(int i = 0; str[i] != '\0'; ++i){
int id = str[i]-'a';
if(p->pt[id] == NULL){
return false;
}
p = p->pt[id];
}
return p->isEnd;
} void getans(char str[])
{
Node* p = root;
for(int i = 0; str[i] != '\0'; ++i){
int id = str[i]-'a';
if(p->pt[id] == NULL){
return;
}
else{
if(p->pt[id]->isEnd && str[i+1] != '\0'){
bool ok = trie_find(str+i+1);
if(ok){
printf("%s\n", str);
return;
}
}
}
p = p->pt[id];
}
} void solve(int n)
{
trie_init();
for(int i = 0; i < n; ++i){
trie_insert(s[i]);
}
for(int i = 0; i < n; ++i){
getans(s[i]);
}
} int main()
{
int n = 0;
while(gets(s[n])){
++n;
}
solve(n);
return 0;
}
HDU 1247 Hat’s Words的更多相关文章
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
- HDU 1247 Hat’s Words(字典树)
http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...
- hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1247 Hat's Words (map+string)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDU 1247 Hat’s Words (字符串匹配,暴力)
题意: 给出一堆单词,如果有一个单词可以分成左右串两个单词,并且在所给的一堆单词中存在,就是hat词,统计所有这样的词,并按字典序输出. 思路: 注意定义,一个hat词可以被两部分已经存在的词组成,那 ...
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDU 1247 Hat’s Words(字典树活用)
Hat's Words Time Limit : 2000 / 1000 MS(Java / Others) Memory Limit : 65536 / 32768 K(Java / Othe ...
- HDU 1247 Hat’s Words(字典树)题解
题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...
随机推荐
- Mutex vs Semaphore
What are the differences between Mutex vs Semaphore? When to use mutex and when to use semaphore? Co ...
- Create a method synchronized without using synchronized keyword
Actually, lots of ways: No need for synchronization at all if you don't have mutable state. No need ...
- C++ Variables and Basic Types Notes
1. Type conversion: If we assign an out-of-range value to an object of unsigned type, the result is ...
- Happy Number
https://leetcode.com/problems/happy-number/ Write an algorithm to determine if a number is "hap ...
- lintcode:First Missing Positive 丢失的第一个正整数
题目 丢失的第一个正整数 给出一个无序的整数数组,找出其中没有出现的最小正整数. 样例 如果给出 [1,2,0], return 3 如果给出 [3,4,-1,1], return 2 挑战 只允许时 ...
- Xamarin.Android 入门之:Android的生命周期
一.前言 活动是Android应用程序的基本构建块,他们可以在许多不同的状态存在.当你把一个Android程序置于后台,过一段时间再打开发现之前的数据还存在. 二.活动状态 下面的图表说明了一个活动可 ...
- *两个关键字static和final
static关键字:可以用于修饰属性,也可以用于修饰方法,还可以用于修饰类. static 修饰属性: 无论一个类生成了多少个对象,所有这些对象共同使用唯一一份静态的成员变量:一个对象对该静态成员变量 ...
- 示例:Servlet读取文件内容并在页面打印输出
package com.mhb; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; impo ...
- C#Winfrom中,窗体加载时会自动执行一次控件的textchange事件,怎么让它不执行?
http://zhidao.baidu.com/link?url=iTSyfa5_RJBSb37S8efdWoL5eDMrnxeAm-prhGSNBXqdP9r7PzNDQTc7gVzJgCNdzli ...
- Android UI开发第三十篇——使用Fragment构建灵活的桌面
http://www.lupaworld.com/article-222973-1.html 当我们设计应用程序时,希望能够尽最大限度的适配各种设备,包括4寸屏.7寸屏. 10寸屏等等,Android ...