hdu1251 题目

这道题,主要是在主函数的输入输出上犹豫了。

#include<stdio.h>
#include<cstring>
#include<iostream>
using namespace std;
#include<algorithm>
const int Max = 1e6+5;
struct Trie{
Trie * next[26];
int cnt;
};
int malloc_p=0;
Trie memory[Max];
Trie *creat()
{
Trie * t = &memory[malloc_p++];
t->cnt=1;
for(int i=0;i<26;i++)
t->next[i]=NULL;
return t;
}
void _insert(Trie *root,char* str)
{
Trie* p=root;
for(int i=0;str[i]!='\0';i++)
{
int k=str[i]-'a';
if(p->next[k]) p->next[k]->cnt++;
else p->next[k]=creat();
p=p->next[k];
}
}
int _search(Trie *root ,char* str)
{
Trie *p = root;
for(int i=0;str[i]!=0;i++)
{
int k = str[i]-'a';
if(p->next[k]) p=p->next[k];
else return 0;
}
return p->cnt;
}
int main()
{
char str[15];
Trie *root = creat(); while(gets(str)&&strlen(str)!=0){
_insert(root,str);} while(cin>>str){
int ans=_search(root,str);
printf("%d\n",ans);
}
return 0;
}

hdu1247 题目

题目大意:

就是找出一个单词,前半部是一个出现过的单词,后半部也是,记住,要严格满足这个条件

所以,其实也就是先查找一个单词的是否有前缀,再用这个单词除去前缀的部分查找是否存在一个这样的单词

虽然题目说按字典序输出,但本身已经是按字典序输入了,所以排序也就省了

#include<stdio.h>
#include<cstring>
#include<iostream>
using namespace std;
#include<algorithm> struct Trie{
Trie * next[26];
int cnt;
}; Trie *root;
int malloc_p=0;
char str[50004][14]; void _insert(char* s)
{
Trie* p=root;
for(int i=0;s[i]!='\0';i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL)
p->next[k]=new Trie();
p=p->next[k];
}
p->cnt=1;
}
int _search2(char* s)
{
Trie *p = root;
for(int i=0;s[i]!='\0';i++)
{
int k = s[i]-'a';
if(p->next[k])
{
p=p->next[k];
if(p->cnt==1&&s[i+1]=='\0')//
return 1;
}
else return 0;
}
return 0;
}
int _search(char* s)
{
Trie *p = root;
for(;*s!='\0';)
{
int k = *s-'a';
if(p->next[k])
{
p=p->next[k];
if(p->cnt==1&& _search2(s+1) )//
return 1;
s++;
}
else return 0;
}
return 0;
}
int main()
{
int i=0;
root = new Trie();
while(cin>>str[i]){
_insert(str[i]);
i++;
}
for(int j=0;j<i;j++){
if(_search(str[j]))
cout<<str[j]<<endl;
} return 0;
}

博客上很多人说这道题挺简单的,但是我还是做了好长时间,可能是因为才开始接触字典树,还不太熟悉吧,

下面是转载的别人的代码:map+string

感觉挺简单的,但是现在还没有学map ,以后应该看得懂吧

  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5. map <string, int> m_v;
  6. string str[50006];
  7. int main() {
  8. int k(-1);
  9. while(cin >> str[++k]) {
  10. m_v[str[k]] = 1;
  11. }
  12. for(int i = 0; i <= k; i++) {
  13. int e = str[i].size()-1;
  14. for(int j = 1; j < e; j++) {
  15. string s1(str[i], 0, j);
  16. string s2(str[i], j);
  17. if(m_v[s1] == 1 && m_v[s2] == 1) {
  18. cout << str[i] << endl;
  19. break;
  20. }
  21. }
  22. }
  23. return 0;
  24. }

hdu1251 && hud 1247 (字典树)的更多相关文章

  1. Hat’s Words HDU - 1247 字典树

    题意:给出数个单词 输出单词 如果该单词 是由字典中的单词组成的 思路:字典树 先把全部建树  然后对于每一个单词进行分割,分别拿两半到字典树里面去找 小心RE! #include<bits/s ...

  2. hdu 1247 (字典树入门)

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

  3. HDU-1251 统计难题,字典树或者map!

    统计难题 很久就看过这个题了,但不会~~~不会~~ 题意:给出一张单词表,然后下面有若干查询,每次给出一个单词,问单词表中是否存在以这个单词为前缀的单词,输出数量.本身也是自身的前缀.只有一组数据! ...

  4. hdu1251 hash或者字典树

    题意: 统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量 ...

  5. hdu1251 统计难题 字典树

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  6. 字典树模板题(统计难题 HDU - 1251)

    https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...

  7. hdu1251(字典树)

    统计难题(hdu1251) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Tota ...

  8. hdoj 1247 Hat’s Words(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...

  9. hdu1251+字典树常用模板

    这里只简单给出几个常用的字典树的模板,要看具体介绍的请看:传送门 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现) ...

随机推荐

  1. System.Data.SqlClient.SqlError: 尚未备份数据库的日志尾部

    SQL还原时出现下面的错误,System.Data.SqlClient.SqlError: 尚未备份数据库 "***" 的日志尾部.如果该日志包含您不希望丢失的工作,请使用 BAC ...

  2. 电脑连接树莓派Pi Zero W

    作者:陈拓 chentuo@ms.xab.ac.cn 2018.05.16/2018.06.09 0.  概述 本位介绍两种电脑连接树莓派Pi Zero W的方法: 电脑通过USB以太网连接树莓派Ze ...

  3. jquery 使用方法(转载)

    原文地址:http://www.cnblogs.com/Chenfengtao/archive/2012/01/12/2320490.html jQuery是目前使用最广泛的javascript函数库 ...

  4. html标签对应的英文原文(转载)

    标签  对应英文 说明 <!--> / 注释 <!DOCTYPE> document type 文档类型 <a> anchor 超链接 <abbr> a ...

  5. 在UNITY中按钮的高亮用POINT灯实现,效果别具一番风味

    在UNITY中按钮的高亮用POINT灯实现,效果别具一番风味

  6. 孤岛营救问题(BFS+状压DP)

    孤岛营救问题 https://www.luogu.org/problemnew/show/P4011 用状压DP标记拿到钥匙的数量 #include<iostream> #include& ...

  7. TZOJ 1242 求出前m大的数(预处理)

    描述 给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=10000)并按从大到小的顺序排列. 输入 ...

  8. 【校招面试 之 C/C++】第15题 C 回调函数

    转自:https://segmentfault.com/a/1190000008293902 做略微改动 什么是回调函数 我们先来看看百度百科是如何定义回调函数的: 回调函数就是一个通过函数指针调用的 ...

  9. runtime - 2 - 使用私有方法

    1. 创建 一个person类, 有对象方法, 私有化方法 2. 在h 文件里面 - (void)eat; //- (void)run:(NSInteger)metre; 3. 在m文件里面 -(vo ...

  10. OC 线程操作2 - NSThread

        方法1 :直接创建 alloc init - (void)createNSThread111{ /* 参数1: (nonnull id) 目标对象 self 参数2:(nonnull SEL) ...