HDU 1251 统计难题(Trie模版题)
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 34909 Accepted Submission(s): 13109
注意:本题只有一组测试数据,处理到文件结束.
3
1
0
题目链接:HDU 1251
以前以为很高级的数据结构,原来就是一个很多后继节点的链表而已,建立过程很简单明了,不懂的话画一个多叉树就知道了……,每一次从表头*root(最近学的是链表,就用*L好了)开始找,然后如果存在就继续找直到遍历完字符串,插入和查找都是这个原理还有最好别用G++提交容易爆内存,C++就不会了
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=26;
const int M=15;
struct Trie
{
Trie *nxt[N];
int cnt;
Trie()
{
CLR(nxt,0);
cnt=0;
}
};
Trie *L=new Trie();
void update(char s[])
{
int len=strlen(s);
int indx;
Trie *cur=L;
for (int i=0; i<len; ++i)
{
indx=s[i]-'a';
if(cur->nxt[indx])
{
cur=cur->nxt[indx];
++(cur->cnt);
}
else
{
Trie *one=new Trie();
++(one->cnt);
cur->nxt[indx]=one; cur=cur->nxt[indx];///新建之后还是要跳到这个新建节点的
}
}
}
int Find(char s[])
{
int len=strlen(s);
Trie *cur=L;
int indx;
for(int i=0; i<len; ++i)
{
indx=s[i]-'a';
if(!cur->nxt[indx])
return 0;
cur=cur->nxt[indx];
}
return cur->cnt;
}
char s[M];
int main(void)
{
while (gets(s)&&strcmp(s,""))
update(s); while (gets(s))
printf("%d\n",Find(s));
return 0;
}
HDU 1251 统计难题(Trie模版题)的更多相关文章
- HDU 1251 统计难题(Trie)
统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...
- hdu 1251 统计难题 trie入门
统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- hdu 1251 统计难题(trie树入门)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU - 1251 统计难题(trie树)
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- HDU 1251 统计难题 (Trie)
pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/ ...
- hdu 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) M ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251 统计难题 (字符串-Trie树)
统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- 一个程序中关于多个osgGA::GUIEventHandler同时存在的问题
平时使用GUIEventHandler不太注意handle()函数的返回值,觉得返回true或者false都无所谓,其实不然. 我遇到的问题是程序中一个节点添加了GUIEventHandler对象pi ...
- eclipse 优化提速
1.windows–>perferences–>general–>startup and shutdown关掉没用的启动项: WTP :一个跟myeclipse差不多的东西,主要差别 ...
- 二、JavaScript语言--JS基础--JavaScript进阶篇--函数
1.什么是函数 函数的作用,可以写一次代码,然后反复地重用这个代码. 如:我们要完成多组数和的功能. var sum; sum = 3+2; alert(sum); sum=7+8 ; alert(s ...
- mysql 超级管理员
mysql> grant all privileges on *.* to 'master'@'%' identified by '3306' with grant option; Query ...
- 在Android中将子View的坐标转换为父View的坐标
在Android中,我们有时候可能会将子View的坐标转换为父View中的坐标.感觉很有用,分享给大家. 在Launcher中有这么一段代码可以完成这项工作. public float getDes ...
- Jquery和雅虎的YQL服务实现天气预报功能!
可以直接使用http://www.tianqi.com/plugin/#tjdm定制天气 很多外部网站都有天气预报功能,对于很多企业内部的门户也需要有天气预报功能,但实现天气预报的功能和方式确有所差异 ...
- android开发 NDK 编译和使用静态库、动态库 (转)
在eclipse工程目录下建立一个jni的文件夹 在jni文件夹中建立Android.mk和Application.mk文件 Android.mk文件: Android提供的一种makefile文件, ...
- Codeforces Gym 100203I I - I WIN 网络流最大流
I - I WINTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.acti ...
- Hark的数据结构与算法练习之地精(侏儒)排序
算法说明 地精排序是交换排序的一种,它是冒泡排序的一种改良,我感觉和鸡尾酒排序挺像的. 不同之处是鸡尾酒排序是从小到大,然后再从大到小切换着排序的.而地精排序是上来先从小到大排序,碰到交换到再从大到小 ...
- mysql ODBC 在64位下提示找不到odbc驱动问题
在64位机器上,如果你想要连接32位mysql ,一般会安装mysql connector/ODBC 64位,并在配置ODBC数据源测试中连接正常,但在程序连接,如ASP.asp.net.VB.Del ...