hdu 1247 (字典树入门)
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12149 Accepted Submission(s): 4338
You are to find all the hat’s words in a dictionary.
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.
ahat
hat
hatword
hziee
word
hatword
思路:先插入每个单词进入字典树,然后分别对每个单词进行查找,find找到前缀后再利用find2找到后缀,当后缀对应某个单词时,则证明此单词由两个字典中单词组成
注意输入到文件尾
package 字典树;
import java.util.Scanner;
class Trie{
private Node root;
public Trie() {
root = new Node();
}
public void insert(String s){
Node t =root;
for(int i=0;i<s.length();i++){
if(t.nodes[s.charAt(i)-'a']==null){
Node node = new Node();
t.nodes[s.charAt(i)-'a'] = node;
}
t = t.nodes[s.charAt(i)-'a'];
}
t.isword = true;
}
public boolean find(String str){
Node t = root;
for(int i=0;i<str.length();i++){
if(t.nodes[str.charAt(i)-'a']!=null){
if(t.isword){
String str1 = str.substring(i, str.length());
//System.out.println(str1);
if(find2(str1)){
return true;
}
}
t = t.nodes[str.charAt(i)-'a'];
}
else return false;
}
return false;
}
private boolean find2(String str) {
Node t = root;
for(int i=0;i<str.length();i++){
if(t.nodes[str.charAt(i)-'a']==null){
return false;
}
t = t.nodes[str.charAt(i)-'a'];
}
if(t.isword) return true;
return false;
}
public
class Node {
Node [] nodes;
boolean isword; //标记末尾
public Node() {
isword = false;
nodes = new Node[26];
}
}
}
public class hdu_1247 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
Trie t = new Trie();
String [] str = new String [50005];
int k=0;
while(sc.hasNext())
{
str[k] = new String();
str[k] = sc.next();
t.insert(str[k++]);
}
for(int i=0;i<k;i++){
if(t.find(str[i])){
System.out.println(str[i]);
}
}
}
}
hdu 1247 (字典树入门)的更多相关文章
- Hat’s Words HDU - 1247 字典树
题意:给出数个单词 输出单词 如果该单词 是由字典中的单词组成的 思路:字典树 先把全部建树 然后对于每一个单词进行分割,分别拿两半到字典树里面去找 小心RE! #include<bits/s ...
- HDU 5687 字典树入门
Problem C Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- HDU 1251 统计难题(字典树入门模板题 很重要)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- hdu 2112(字典树+最短路)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2072(字典树模板,set,map均可做)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
随机推荐
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- group by多字段分组
在平时的开发任务中我们经常会用到MYSQL的GROUP BY分组, 用来获取数据表中以分组字段为依据的统计数据.比如有一个学生选课表,表结构如下: Table: Subject_Selection S ...
- bzoj 2457 [BeiJing2011]双端队列 模拟+贪心
[BeiJing2011]双端队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 457 Solved: 203[Submit][Status][D ...
- 学习opencv-------函数使用一
#include"head.h" //cvResize() /*int main() { IplImage *img = cvLoadImage("e:/picture/ ...
- ACM2112迪克斯特算法
HDU Today Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区50 ...
- Bigbluebutton安装过程
BigBlueButton安装过程(翻译) 欢迎来到BigBlueButton 1.0-beta安装指南(以下简称BigBlueButton 1.0).BigBlueButton是一个开放源代码的网络 ...
- 允许Traceroute探测
允许Traceroute探测 漏洞描述: 允许Traceroute探测 漏洞描述 本插件使用Traceroute探测来获取扫描器与远程主机之间的路由信息.攻击者也可以利用这些信息来了解目标网络的网络拓 ...
- bug级别分类
bug级别分类 2014-10-20 10:02 6403人阅读 评论(0) 收藏 举报 分类: SQA(17) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 由 ...
- C11简洁之道:lambda表达式
1. 定义 lambda表达式是C++11非常重要也是很常用的特性之一,来源于函数式编程的概念,也是现代编程语言的一个特点.它有如下特点: 声明式编程风格:就地匿名定义目标函数或者函数,不需要额外写 ...
- jsp03( javabeans)
1.javabean简介 Javabeans就是符合某种特定规范Java类.使用Javabeans的好处是[解决代码的重复编写],减少代码冗余,功能区分明确,提高代码的维护性. 2.javabean的 ...