Description

在一个奇怪的村子中,很多人的名字都很长,比如aaaaa, bbb and abababab。
名字这么长,叫全名显然起来很不方便。所以村民之间一般只叫名字的前缀。比如叫'aaaaa'的时候可以只叫'aaa',因为没有第二个人名字的前三个字母是'aaa'。不过你不能叫'a',因为有两个人的名字都以'a'开头。村里的人都很聪明,他们总是用最短的称呼叫人。输入保证村里不会有一个人的名字是另外一个人名字的前缀(作为推论,任意两个人的名字都不会相同)。
如果村里的某个人要叫所有人的名字(包括他自己),他一共会说多少个字母?
Input
输入第一行为数据组数T (T<=10)。每组数据第一行为一个整数n(1<=n<=1000),即村里的人数。以下n行每行为一个人的名字(仅有小写字母组成)。输入保证一个村里所有人名字的长度之和不超过1,000,000。
 Output

对于每组数据,输出所有人名字的字母总数。

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 最短的名字(字典树模板题)的更多相关文章

  1. 字典树模板题(统计难题 HDU - 1251)

    https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...

  2. CH 1601 - 前缀统计 - [字典树模板题]

    题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...

  3. HDU - 1251 字典树模板题

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部 ...

  4. HDU 1251 统计难题(字典树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...

  5. P1184 高手之在一起(字典树模板题,hash算法, map)

    哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...

  6. HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...

  7. (模板)hdoj1251(字典树模板题)

    题目链接:https://vjudge.net/problem/HDU-1251 题意:给定一系列字符串之后,再给定一系列前缀,对每个前缀查询以该字符串为前缀的字符串个数. 思路: 今天开始学字典树, ...

  8. CSU 1115 最短的名字

    传送门 Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description 在一个奇怪的 ...

  9. hdu 1251 字典树模板题 ---多串 查找单词出现次数

    这道题题目里没有给定数据范围 我开了2005  疯狂的WA 然后开了50000, A掉  我以为自己模板理解错  然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...

随机推荐

  1. [转]Oracle - 数据库的实例、表空间、用户、表之间关系

    本文转自:http://www.cnblogs.com/adforce/p/3312252.html 完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物 ...

  2. LN : leetcode 413 Arithmetic Slices

    lc 413 Arithmetic Slices 413 Arithmetic Slices A sequence of number is called arithmetic if it consi ...

  3. Echarts生成饼状图、条形图以及线形图 JS封装

    1.在我们开发程序中,经常会用到生成一些报表,比方说饼状图,条形图,折线图等.不多说了,直接上封装好的代码,如下Echarts.js所示 以下代码是封装在Echarts.js文件中 /** * Cre ...

  4. 在WIndowsPhone8 上制作的简单的计算器

    今天,闲着没事,就自己做了一个小小的计算器...虽说自己刚学wp8开发没多长时间,望大神多多指教..1.这是前台页面的代码 <Grid x:Name=" Margin="10 ...

  5. Android开发中使用startActivityForResult()方法从Activity A跳转Activity B出现B退出时A也同时退出的解决办法

    最近一个 App 中用到了 startActivityForResult() 方法,使用的时候却出现了一些问题,比如我在 Activity A 中调用该方法向 Activity B 中跳转,如果 B  ...

  6. nginx for ubuntu

    1.创建文件夹 :mkdir nginx 2.解压nginx: tar zxvf nginx.gz.tar 3.nginx 初始化:在nginx的路径下执行:./configure 有可能会报错: . ...

  7. R Programming week 3-Debugging

    Something’s Wrong! Indications that something’s not right message: A generic notification/diagnostic ...

  8. 快速开发框架天梭(Tissot)

    天梭(Tissot)集成SpringBoot+Dubbo等主流互联网技术栈,高度集成.优化方便快速搭建应用.某互金科技公司内部孵化框架,已应用于公司90%业务系统. 框架划分模块有: tissot-c ...

  9. WebSocket 的一些简单页面推送使用

    因为做通信项目的时候,需要实时获取每个分机的当前状态,发现websocket还不错,只是对浏览器的要求比较高, 针对特定用户推送消息,网上有一些 public class GetHttpSession ...

  10. 设计模式之一:strategy pattern

    定义算法族,彼此之间可以替换.变化的方法抽出封装,不变的父类定义继承.多用组合少用继承. 代码示例先不贴了.