【统计难题】【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近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- Spring MVC -- 下载文件
像图片或者HTML文件这样的静态资源,在浏览器中打开正确的URL即可下载,只要该资源是放在应用程序的目录下,或者放在应用程序目录的子目录下,而不是放在WEB-INF下,tomcat服务器就会将该资源发 ...
- QuickSortDemo
package com.suning.sntcscase.controller.MutiThread; import static jdk.nashorn.internal.objects.Globa ...
- SonarQube - 安装与运行SonarQube
1 - 下载SonarQube SonarQube有多个版本,其中CE(Community Edition)版本免费开源,其余的开发者版本.企业版本和数据中心版本都是收费版本. 官网下载:https: ...
- oracle登录信息对应
登录数据库填写的是,配置文件中自己定义的值 2. 查询非纯数字值的字段内容 select trim(translate(RTRIM(LTRIM(ITEM_NUMBER)), '#0123456789' ...
- canal使用
报错信息:com.alibaba.druid.pool.DruidDataSource - testWhileIdle is true, validationQuery not set 解决方法: 找 ...
- .whl文件安装cuda10.0版本的pytorch1.3.0+torchvision0.4.1
$ python3 -m venv env3$ source env3/bin/activate$ cd env3/share/python-wheels 在此找到对应的版本:https://down ...
- Python 基础 while 循环
Python 基础 while 循环 while 循环 在生活中,我们遇到过循环的事情吧?比如循环听歌.在程序中,也是存才的,这就是流程控制语句 while 基本循环 while 条件: # 循环体 ...
- phpdocmentor 生成php 开发文档(转载)
PHPDocumentor是一个用PHP写的工具,对于有规范注释的php程序,它能够快速生成具有相互参照,索引等功能的API文档.老的版本是phpdoc,从1.3.0开始,更名为phpDocument ...
- Java ClassLoader 学习理解
/** * <html> * <body> * <P> Copyright 1994 JsonInternational</p> * <p> ...
- C#泛型集合之——哈希集合
1.特点:HashSet 中元素不重复,容量为元素个数,自动增大.是一组值,是高性能的数学集合. 2.创建: (1)HashSet<类型> 集合名 = new HashSet<类型& ...