CSU - 1115 最短的名字(字典树模板题)
Description
对于每组数据,输出所有人名字的字母总数。
Sample Input
1
3
aaaaa
bbb
abababab Sample Output
5
http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2081&pid=15
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1010
#define maxv 1010
#define mod 1000000000
using namespace std; typedef struct node
{
int count;
struct node *next[];
}*tree; void insert(tree h,char *s)
{ //每一个节点保存的都是每一个字母出现的次数。
tree p=h;
int len=strlen(s);
for(int i=;i<len;i++)
{
int index=s[i]-'a';
if(p->next[index]!=NULL)
{
p=p->next[index];
p->count++;
}
else
{
tree tem=(tree)calloc(,sizeof(node));
tem->count=;
p->next[index]=tem;
p=tem;
}
}
} int find(tree h)
{ //查找不为空的结点,加上相应的次数,如果count值大于1,则说明有分支,需继续查找下去
tree p=h;
int sum=,i;
for(int i=;i<;i++)
{
if(h->next[i]!=NULL)
{
p=h->next[i];
sum+=p->count;
if(p->count>) sum+=find(p);
}
}
return sum;
} char s[]; int main()
{
//freopen("a.txt","r",stdin);
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
tree head=(tree)calloc(,sizeof(node));
while(n--)
{
scanf("%s",s);
insert(head,s);
}
printf("%d\n",find(head));
free(head);
}
return ;
}
CSU - 1115 最短的名字(字典树模板题)的更多相关文章
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- CH 1601 - 前缀统计 - [字典树模板题]
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- HDU 1251 统计难题(字典树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...
- P1184 高手之在一起(字典树模板题,hash算法, map)
哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...
- HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...
- (模板)hdoj1251(字典树模板题)
题目链接:https://vjudge.net/problem/HDU-1251 题意:给定一系列字符串之后,再给定一系列前缀,对每个前缀查询以该字符串为前缀的字符串个数. 思路: 今天开始学字典树, ...
- CSU 1115 最短的名字
传送门 Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description 在一个奇怪的 ...
- hdu 1251 字典树模板题 ---多串 查找单词出现次数
这道题题目里没有给定数据范围 我开了2005 疯狂的WA 然后开了50000, A掉 我以为自己模板理解错 然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...
随机推荐
- ES6知识点汇总
MDN镇楼: https://developer.mozilla.org/zh-CN/ 1.ES6新添加数据类型:symbol ----------- https://developer.moz ...
- JS操作CSS
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 解决 图片在div中等比例缩放问题 (未解决:图片比例小于盒子模型时不会自动填充)
如题,该方案仅支持对图片等比例缩放.本文附件地址:https://files.cnblogs.com/files/john69-/background-Img.rar <!DOCTYPE htm ...
- 解决::processDebugResourcesERROR: In<declare-styleable> FontFamilyFont编译报错
cordova编译时报错 错误信息 :processDebugResourcesERROR: In <declare-styleable> FontFamilyFont, unable t ...
- 在阿里云上搭建nginx + ThinkPHP 的实践
作为一个程序猿,理应用linux系统来作为平时的工作机环境,哎,之前倒是用过一段时间的linux,可惜后来换了本本,后来竟然没有保持,嗷嗷后悔中... 废话不多说,大家用windows的理由都一样,但 ...
- ESSENTIALS OF PROGRAMMING LANGUAGES (THIRD EDITION) :编程语言的本质 —— (一)
# Foreword> # 序 This book brings you face-to-face with the most fundamental idea in computer prog ...
- C#方法参数关键字
一.params关键字 prams告诉函数的调用者,该函数的参数数量是可变,如果调用函数的参数标识了params关键字,那么我们可以使用逗号分割的参数或者一个数组来作为参数: 1.这里只能是数组,Li ...
- Farseer.net轻量级ORM开源框架 V1.x 入门篇:表实体类映射
导航 目 录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:数据库上下文 下一篇:Farseer.net轻量级ORM开源框 ...
- SCHTASKS /CREATE
SCHTASKS "/" 这个符号前要加个空格才能运行成功 ,搞半天!
- CSS 文字换行与不换行
1. 强制不换行 p{ white-space:nowrap; } 2. 自动换行 p{ word-wrap: break-word; word-break: normal; } 3. 强制英文单词断 ...