思路

题意题目为中文题,这里不再过多阐述。

思路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打表或字典树】【字典树模板】的更多相关文章

  1. AC日记——统计难题 hdu 1251

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

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

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

  3. 统计难题 HDOJ --1251

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

  4. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  5. HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

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

  6. hdu 1251:统计难题[【trie树】||【map】

    <题目链接> 统计难题                        Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131 ...

  7. HDU 1251 统计难题(Trie模版题)

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

  8. hdu 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    M ...

  9. [ACM] hdu 1251 统计难题 (字典树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

随机推荐

  1. Jenkins集成TestNG

    1.Jenkins安装插件 TestNG Results Plugin 2. 添加配置 “构建后操作”->“Publish TestNG Results” 保持默认配置即可 3.查看报告 项目构 ...

  2. Android存储及getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()区别:

    存储介绍: Android系统分为内部存储和外部存储,内部存储是手机系统自带的存储,一般空间都比较小,外部存储一般是SD卡的存储,空间一般都比较大,但不一定可用或者剩余空间可能不足.一般我们存储内容都 ...

  3. [转]System Verilog的概念以及与verilog的对比

    原文地址: http://blog.csdn.net/gtatcs/article/details/8970489 SystemVerilog语言简介 SystemVerilog是一种硬件描述和验证语 ...

  4. redis学习(二)——案例练习

    案例需求: 1.提供index.html页面,页面中有一个省份下拉列表 2.当页面加载完成后发送ajax请求,加载所有省份 3.列表中的省份保持不变,则之后每次刷新页面都是从redis中获取 * 注意 ...

  5. Java开发笔记(一百五十一)Druid连接池的用法

    C3P0连接池自诞生以来在Java Web领域反响甚好,业已成为hibenate框架推荐的连接池.谁知人红是非多,C3P0在大型应用场合中暴露了越来越多的局限性,包括但不限于下列几点:1.C3P0管理 ...

  6. STVD使用printf输出数据错误

    使用STM8L052输出调试信息 重定向put char #include "stdio.h" //必不可缺少 char putchar (char c) { /* Write a ...

  7. redis源码分析(二)-rio(读写抽象层)

    Redis io抽象层 Redis中涉及到多种io,如socket与file,为了统一对它们的操作,redis设计了一个抽象层,即rio,使用rio可以实现将数据写入到不同的底层io,但是接口相同.r ...

  8. JVM 参数调优配置

    在 tomcat 配置文件 tomcat/bin/catalina.sh 中 配置  JAVA_OPTS="-server -Xms2048m -Xmx2048m -Xss1024K -XX ...

  9. (转)微服务_.NET Core Consul服务发现与治理

    原文地址:https://www.cnblogs.com/waynechan/p/9354909.html Consul官网:https://www.consul.io Consul下载地址:http ...

  10. SpringCloud中服务发现-Eureka

    1.Eureka服务端集群开发 1.先创建一个父工程 若是不是普通demo,还有别的配置时,需要注意若是服务开不起来可能就是父类依赖中可能会需要<dependencyManagement> ...