HDU 1251 Trie树模板题
1、HDU 1251 统计难题 Trie树模板题,或者map
2、总结:用C++过了,G++就爆内存。。
题意:查找给定前缀的单词数量。
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstdio>
#define max(a,b) a>b?a:b
#define F(i,a,b) for(int i=a;i<=b;i++)
#define mes(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=,MAX=; struct Node
{
int count;
Node *child[];
Node(){
mes(child,NULL);
count=;
}
}; Node *root=new Node,*current;
void insert(char *str)
{
current=root;
for(int i=;str[i];++i){
int m=str[i]-'a';
if(current->child[m]==NULL){
current->child[m]=new Node;
}
current=current->child[m];
current->count++;
}
} int search(char *str)
{
current=root;
for(int i=;str[i];++i){
int m=str[i]-'a';
if(current->child[m]==NULL)
return ;
current=current->child[m];
}
return current->count;
} int main()
{
char str[];
while(gets(str),*str)
insert(str);
while(gets(str))
printf("%d\n",search(str)); return ;
}
HDU 1251 Trie树模板题的更多相关文章
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- hdu 1251 字典树模板题 ---多串 查找单词出现次数
这道题题目里没有给定数据范围 我开了2005 疯狂的WA 然后开了50000, A掉 我以为自己模板理解错 然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...
- HDU 1251 统计难题 (Trie树模板题)
题目链接:点击打开链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单 ...
- poj3630 Phone List (trie树模板题)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26328 Accepted: 7938 Descr ...
- hdu 1251 trie树
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Problem De ...
- hdu 1754 线段树模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 #include <cstdio> #include <cmath> # ...
- hdu 2665 划分树模板题(可作为模板)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 835. 字符串统计(Trie树模板题)
维护一个字符串集合,支持两种操作: “I x”向集合中插入一个字符串x: “Q x”询问一个字符串在集合中出现了多少次. 共有N个操作,输入的字符串总长度不超过 105105,字符串仅包含小写英文字母 ...
- hihocoder_1014: Trie树(Trie树模板题)
题目链接 #include<bits/stdc++.h> using namespace std; ; struct T { int num; T* next[]; T() { num=; ...
随机推荐
- [LeetCode] Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- NPOI 通用导出数据到Excel 分类: C# Helper 2014-11-04 16:06 246人阅读 评论(0) 收藏
应用场景: 在项目中,经常遇到将数据库数据导出到Excel,针对这种情况做了个程序封装.工作原理:利用NPOI将SQL语句查询出的DataTable数据导出到Excel,所见即所得. 程序界面: ...
- 第十三篇:在SOUI中使用有窗口句柄的子窗口
前言: 无论一个DirectUI系统提供的DUI控件多么丰富,总会有些情况下用户需要在DUI窗口上放置有窗口句柄的子窗口. 为了和无窗口句柄的子窗口相区别,这里将有窗口句柄的子窗口称之为真窗口. 每一 ...
- C语言里面捕获错误机制
在C语言中异常处理一般有这么几种方式: 1.使用标准C库提供了abort()和exit()两个函数,它们可以强行终止程序的运行,其声明处于<stdlib.h>头文件中. 2.使用asser ...
- 【荐】Spring事务配置的五种方式
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...
- ios 数据类型转换 UIImage转换为NSData NSData转换为NSString
1.UIImage转换为NSData NSData *data;if (UIImagePNGRepresentation(image) == nil) { data = UIImageJPEGRepr ...
- filter应用案例四:页面静态化
1 说明 你到"当当"搜索Java分类图书时,"当当"会去查询数据库!每天都有很多人去搜索"Java分类"的图书,每次都去访问数据库,这会有 ...
- Java ClassLoader基础及加载不同依赖 Jar 中的公共类
转载自:最新内容及最清晰格式请见 http://www.trinea.cn/android/java-loader-common-class/ 本文主要介绍 ClassLoader 的基础知识,Cla ...
- poj2385 dp(递推)
题目链接 :http://bak3.vjudge.net/contest/136499#problem/D 题意: //转移方程dp[i][j]=max(dp[i-1][j],dp[i-1][j-1] ...
- HDU 4858 项目管理 分块
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4858 题解: 下面说一个插入查询时间复杂度为sqrt(m)的算法: 对每个点定义两个值:val,su ...