hdu 1251:统计难题[【trie树】||【map】
统计难题
注意:本题只有一组测试数据,处理到文件结束.
#include <cstdio>
#include <malloc.h>
#include <cstring>
#include <algorithm>
using namespace std; char s[];
struct node{
int cnt;
node *next[]; //该节点的所有子节点
void init(){ //初始化该节点
cnt=;
for(int i=;i<;i++)
next[i]=NULL;
}
};
void insert(node *root,char *s){
node *p =root ,*now;
for(int i=;s[i];i++){
int tmp=s[i]-'a';
if(p->next[tmp]==NULL){ //如果该节点为空,则创建一个新的节点
p->next[tmp]=new node;
p=p->next[tmp];
p->init();
}
else p=p->next[tmp]; //如果该节点不空,则继续向下插入
p->cnt++; //以该节点结尾的前缀个数+1
}
}
int find(node *root,char *s){
node *p=root;
for(int i=;s[i];i++){
int tmp=s[i]-'a';
p=p->next[tmp];
if(!p)return ; //如果遇到不满足的节点,就直接结束
}
return p->cnt;
}
int main(){
node *root=new node;
root->init();
while(gets(s)&&strlen(s))
insert(root,s);
while(gets(s))
printf("%d\n",find(root,s));
return ;
}
trie树数组实现:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 1e6+;
int trie[N][]; //trie[now][next]代表第一次遍历到该节点的序号
int num[N]={},pos=;
void Insert(char *str){
int now=;
for(int i=;str[i];i++){
int next=str[i]-'a';
if(!trie[now][next])trie[now][next]=++pos; //如果没有遍历过该节点,就给该节点赋值(相当于给trie树创建一个节点)
now=trie[now][next];
num[now]++; //该前缀的数量+1
}
}
int Search(char *str){
int now=;
for(int i=;str[i];i++){
int next=str[i]-'a';
if(!trie[now][next])return ; //只要一找到不符合的节点,就直接返回0
now=trie[now][next]; //当前节点继续向下匹配
}
return num[now];
}
int main(){
char str[];
while(gets(str)&&strlen(str))
Insert(str);
while(gets(str))
printf("%d\n",Search(str));
return ;
}
map做法
#include <cstdio>
#include <string>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std; int main()
{
string s, str[];
int cur = ;
map<string, int>mapp;
while (getline(cin, s) && s[] != '\0') //注意是用这个来判断空行,首先一定要用getline读入,其次是s[0]=='\0'时表示是空行
{
string ss;
for (int i = ; i <=s.length(); i++) {
ss = s.substr(, i); //获得字符串s中从第0位开始的长度为i的字符串
mapp[ss]++;
}
}
while (cin >> s)
{
printf("%d\n", mapp[s]);
}
return ;
}
hdu 1251:统计难题[【trie树】||【map】的更多相关文章
- HDU - 1251 统计难题(trie树)
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- hdu 1251 统计难题(trie树入门)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- hdu 1251 统计难题 trie入门
统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- HDU 1251 统计难题(Trie)
统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...
- hdu 1251 统计难题 字典树第一题。
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 1251 统计难题 字典树大水题
今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...
随机推荐
- 列式数据库~clickhouse 数据同步使用
一 简介:进一步了解clickhouse二 数据操 1 单机建表 create TABLE aaa ( id UInt32, uid UInt32, amount Float64, ...
- More Effective C++ 条款0,1
More Effective C++ 条款0,1 条款0 关于编译器 不同的编译器支持C++的特性能力不同.有些编译器不支持bool类型,此时可用 enum bool{false, true};枚举类 ...
- 2018-2019-2 网络对抗技术 20165227 Exp3 免杀原理与实践
2018-2019-2 网络对抗技术 20165227 Exp3 免杀原理与实践 **免杀** - 一般是对恶意软件做处理,让它不被杀毒软件所检测.也是渗透测试中需要使用到的技术. - 要做好免杀,就 ...
- Linux用户相关指令
⒈添加用户 ①useradd [Options] 用户名 useradd -d 指定用户目录 用户名 useradd -g 用户组 用户名 ⒉指定/修改用户密码 ①passwd 用户名 ⒊删除用户(建 ...
- Dubbo监控中心
(1).dubbo-admin(管理控制台) 1).从https://github.com/apache/incubator-dubbo-ops下载解压 2).修改dubbo-admin配置文件中zo ...
- centos6安装python3.6.4
安装Python依赖包: [root@Python src]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlit ...
- 存储器结构、cache、DMA架构分析--【原创】
存储器的层次结构 高速缓冲存储器 cache 读cache操作 cache如果包含数据就直接从cache中读出来,因为cache速度要比内存快 如果没有包含的话,就从内存中找 ...
- C# 使用Win32 API将1个EXE程序嵌入另1个程序中
已经干到天快亮了,就不废话直接贴点儿代码吧 ; ; /// <summary> /// 查找窗口 ///第一个参数是窗口的标题,第二个参数可直接用 null ///通过窗口的标题查找对应的 ...
- oracle客户端安装
一.引导安装并配置 1)下载客户端(两个文件) 2)选中两个压缩包解压到同一个文件夹下 3)点击setup 4)选择:创建和配置数据库桌面类选择安装目录,全局数据库名:orcl,密码admin口令管理 ...
- centos7 部署 docker ce
=============================================== 2019/4/9_第1次修改 ccb_warlock === ...