【统计难题】【HDU - 1251】【map打表或字典树】【字典树模板】
思路
题意:题目为中文题,这里不再过多阐述。
思路1:可以在读入单词表的过程中将单词分解,用map将它一 一记录
思路2:利用字典树,这个方法较快些,下面代码中会分别给出数组和结构体指针两种形式的字典树,指针形式的有时可能会因题目内存限制而导致Memory Limit Exceeded,这时就可选择数组形式的。不过对于本题来说不用担心。
AC代码
代码1:map打表
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
map<string, int> table;
int main()
{
std::ios::sync_with_stdio(false);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
table.clear();
string s;
while(getline(cin, s))
{
if(s[0] == '\0')
break;
string ss = "";
for(int i = 0; i < s.size(); i++)
{
ss += s[i];
table[ss]++;
}
}
while(cin >> s)
{
cout << table[s] << endl;
}
}
代码2:数组形式的字典树
#include<bits/stdc++.h>
using namespace std;
const int maxnode = 400001;
const int maxs = 27;
char s[10 + 10];
int trie[maxnode][maxs] ;
int sum[maxnode] ;
int node = 0;
void inserts(char *t)
{
int len = strlen(t);
int cur = 0;
for(int i = 0; i < len; i++)
{
int p = t[i] - 'a';
if(!trie[cur][p])
trie[cur][p] = ++node;
sum[trie[cur][p]]++;
cur = trie[cur][p];
}
}
int searchs(char *t)
{
int len = strlen(t);
int cur = 0;
for(int i = 0; i < len; i++)
{
int p = t[i] - 'a';
if(!trie[cur][p])
return 0;
cur = trie[cur][p];
}
return sum[cur];
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
memset(trie, 0, sizeof(trie));
memset(sum, 0, sizeof(sum));
while(gets(s) != NULL)
{
if(s[0] == '\0')
break;
inserts(s);
}
while(scanf("%s", s) != EOF)
{
cout << searchs(s) << endl;
}
}
代码3:结构体指针形式的字典树
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[10+10];
struct Trie
{
Trie* next[26];
int sum;
Trie()
{
for(int i = 0; i < 26; i++)
next[i] = NULL;
sum = 0;
}
}root;
void inserts(char *s)
{
Trie* p = &root;
int len = strlen(s);
for(int i = 0; i < len; i++)
{
int cur = s[i] - 'a';
if(p->next[cur] == NULL)
{
p->next[cur] = new Trie;
}
p = p->next[cur];
p->sum++;
}
}
int finds(char *s)
{
Trie* p = &root;
int len = strlen(s);
for(int i = 0; i < len; i++)
{
int cur = s[i] - 'a';
if(p->next[cur] == NULL)
return 0;
else
p = p->next[cur];
}
return p->sum;
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
while(gets(s) != NULL)
{
if(s[0] == '\0')
break;
inserts(s);
}
while(scanf("%s", s) != EOF)
{
cout << finds(s) << endl;
}
}
【统计难题】【HDU - 1251】【map打表或字典树】【字典树模板】的更多相关文章
- AC日记——统计难题 hdu 1251
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- 统计难题 HDOJ --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. ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- hdu 1251:统计难题[【trie树】||【map】
<题目链接> 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131 ...
- HDU 1251 统计难题(Trie模版题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- hdu 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) M ...
- [ACM] hdu 1251 统计难题 (字典树)
统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- Centos7安装部署Rabbitmq教程
依赖关系: 版本依赖一定要提前看清楚. RabbitMQ相关版本依赖关系查看 https://www.rabbitmq.com/which-erlang.html 可以看到要求版本Erlang21.3 ...
- Centos7——无法访问Windows系统的分区
我的Windows分区是Ntfs格式的,所以我直接安装ntfs即可解决年 yum -y install ntfs*
- Git 配置环境
安装 在Fedora/CentOS下安装 $ sudo yum install git 在Debian/Ubuntu Linux下载安装 $ sudo apt-get install git Wind ...
- 解决 plsql 启动报错 Initialization error
由于文件 oci.dll 路径没添加 1.点击edit 2.点击PL/SQL 3.弹框中 填写oci.dll文件的完整路径!完整路径!完整路径!完整路径!完整路径!完整路径!完整路径!完整路径 ...
- LeetCode 421. 数组中两个数的最大异或值(Maximum XOR of Two Numbers in an Array) 71
421. 数组中两个数的最大异或值 421. Maximum XOR of Two Numbers in an Array 题目描述 给定一个非空数组,数组中元素为 a0, a1, a2, - , a ...
- sqlserver 数据类型 C# clr 数据类型 映射
https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/sql/linq/sql-clr-type-mapping#default- ...
- python3 安装 pyinstaller 时报错的解决办法
如上图所示,在安装的过程中发现是所关联的一个 future模块安装失败,庵后我有单独安装了一下这个future,发现还是失败 然后在网上寻找解决办法,最后找到了这个指令,pip install fut ...
- GO指南练习:切片
最近开始GO语言的学习,在GO指南中练习到切片这个题目 练习:切片 实现 Pic.它应当返回一个长度为 dy 的切片,其中每个元素是一个长度为 dx,元素类型为 uint8 的切片.当你运行此程序时, ...
- jQuery格式化显示json数据
一.概述 JSONView 在gitlab上面,有一个jQuery JSONView插件,地址为:https://github.com/yesmeck/jquery-jsonview demo地址:h ...
- 解决本地Bootstrap字体图标不可见的问题
原文:https://www.jianshu.com/p/70ac459d33e7 作为Bootstrap的初学者,我最近遇到了一个问题:在使用Bootstrap字体图标时,图标不可见.使用代码如下: ...