poj 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<cmath>
using namespace std;
char ss[][];
#define MAX 27
struct trie
{
trie *next[MAX];
int v;
trie()
{
int i;
v=;
for(i=; i<; i++) next[i]=NULL;
}
};
trie *p,*q;
void creattrie(char *str,trie *root)
{
int len = strlen(str);
p = root;
for(int i=;i<len; i++)
{
int id = str[i] - 'a';
if(p->next[id] == NULL)
{
q=new trie;
q->v = ;
p->next[id] = q;
p=q;
}
else
{
p=p->next[id];
p->v+=;
}
}
}
int findtrie(char *str,trie *root)
{
int i;
int len = strlen(str);
p = root;
for(i=;i<len;i++)
{
int id = str[i] - 'a';
p=p->next[id];
if(p->v == )
{
return i+;
}
}
}
int main()
{
int T,n,ans;
scanf("%d",&T);
while(T--)
{
ans = ;
trie *root = new trie;
scanf("%d",&n);
//getchar();
for(int i=;i<n;i++)
{
scanf("%s", ss[i]);//用gets接收错了一天了,晚上才发现
creattrie(ss[i],root);
}
for(int i=;i<n;i++)
{
int kk = findtrie(ss[i],root);
ans = ans+kk;
}
printf("%d\n",ans);
}
return ;
}
poj 1251 统计难题(字典树)的更多相关文章
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- 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 统计难题 这道题唯一的 ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- hdoj 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路分析:该问题要求求出以某个字符串为前缀的单词数目,通过使用字典树,在字典树中添加count记 ...
- HDU 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251统计难题 字典树
字典树的应用. 数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊.赶紧来熟悉一下字典树. 空间开销太大T T #include<cstdio> #include<cstrin ...
- hdu -1251 统计难题(字典树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...
随机推荐
- Java中相等测试
一:equals与==的区别 (1)基本数据类型 byte,short,char,int,long,float,double,boolean 此类数据类型的比较需要使用==,此时比较的是他们的值,若相 ...
- Qt_Window@Qt Command Prompt从命令行创建工程
#include <QApplication> #include <QLabel> int main(int argc, char *argv[]) { QApplicatio ...
- Spring源码学习之:spring注解@Transactional
在分析深入分析@Transactional的使用之前,我们先回顾一下事务的一些基本内容. 事务的基本概念 先来回顾一下事务的基本概念和特性.数据库事务(Database Transaction) ,是 ...
- 内存使用空间之swap建置[转]
http://www.cnblogs.com/ggjucheng/archive/2012/08/22/2651502.html 内存置换空间(swap)之建置 安装时一定需要的两个 partitio ...
- Unity脚本系统
什么是脚本?脚本是一个 MonoBehavior, 继承关系是 MonoBehavior -> Behavior -> Component -> Object GameObject ...
- Python3下map函数的显示问题
map函数是Python里面比较重要的函数,设计灵感来自于函数式编程.Python官方文档中是这样解释map函数的: map(function, iterable, ...) Return an it ...
- unity, Additive Animtion注意事项
以下摘自官方文档:(http://docs.unity3d.com/Manual/AnimationScripting.html) Additive animations allow you to o ...
- 螺旋矩阵 noip2014普及组
本题可以直接模拟填数字,也可以直接计算结果. 代码一:(这个代码,缺陷在于数组太大,浪费内存啊.另外,循环次数也不少.总之,时间空间的消耗都不小.) /*======================= ...
- const, static and readonly
const, static and readonly http://tutorials.csharp-online.net/const,_static_and_readonly Within a cl ...
- CSS3之Flexbox布局
CSS3为我们提供了一种可伸缩的灵活的web页面布局方式-flexbox布局,它具有很强大的功能,可以很轻松实现很多复杂布局,在它出现之前,我们经常使用的布局方式是浮动或者固定宽度+百分比来进行布局, ...