【统计难题】【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近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- 小程序报错:对应的服务器 TLS 为 TLS 1.0 ,小程序要求的 TLS 版本必须大于等于 1.2
我这里出现此错误的原因是,搭载域名网站的服务器是windows2008 r2,配置的域名证书是TLS1.0版本,需要在服务器注册表中加入TLS的其他版本. 处理办法如下 小程序报错 TLS 版本必须大 ...
- url、href、src
一.URL的概念 统一资源定位符(或称统一资源定位器/定位地址.URL地址等,英语:Uniform Resource Locator,常缩写为URL),有时也被俗称为网页地址(网址).如同在网络上的门 ...
- tracking.js实现前端人脸识别
1.下载https://trackingjs.com/ 2.运行例子 纳总一下 发现效果 里面的代码为 <!doctype html><html><head> &l ...
- 多分类评测标准(micro 和 macro)
- python数据分析2之numpy
源代码 # -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. " ...
- 三层交换,单臂路由,vtp
- (转)Intellij IDEA 2017 debug断点调试技巧与总结详解篇
背景:详细介绍idea的debug调试过程 Intellij IDEA 2017 debug断点调试技巧与总结详解篇
- Vue 公众号开发 (菜鸡前段的血泪史)
首先vue-cli就不说了 接下来要说我们需要注意什么 公众号的每个页面都有一个title 所以我们在开发过程中 需要插件 安装vue-wechat-title 安装vue-js-sdk
- pytest_03_pycharm运行pytest (转:上海悠悠)
前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...
- Scala 系列(七)—— 常用集合类型之 Map & Tuple
一.映射(Map) 1.1 构造Map // 初始化一个空 map val scores01 = new HashMap[String, Int] // 从指定的值初始化 Map(方式一) val s ...