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. [转]windows azure How to use Blob storage from .NET

    本文转自:http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/?rnd=1 ...

  2. [BZOJ2809][Apio2012]dispatching 贪心+可并堆

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 我们考虑以每一个节点作为管理者所得的最优答案,一定是优先选择所要薪水少的忍者.那么首 ...

  3. Javascript数据结构之栈

    作者原文:http://hawkzz.com/blog/blog/1515054561771 定义 栈是一种特殊的列表,栈内的元素只能通过列表的一端访问,这一端称为栈顶.栈被称为一种先入后出的数据结构 ...

  4. oracle 时间格式转化以及计算

    --A表中的日期字段 create_date   例如:2017-08-05  转化为2017年8月5日   oracle 在这里的双引号会忽略 select to_char(to_date(tt.c ...

  5. 在action中将字符串、对象、list集合保存到值栈中,在jsp页面中获取的方法

    转自:csdn 封装对象User,属性有id,username,email等1.1:在action中将字符串保存到值栈中   1.1.1 获取值栈对象         ValueStack stack ...

  6. IIS ARR设置HTTP跳转到HTTPS

    GUI Version - Select the website you wish to configure- In the “Features View” panel, double click U ...

  7. 遮罩 HUD 指示器 蒙板 弹窗

    遮罩 HUD 指示器 蒙板 弹窗 UIAlertView的使用<代理方法处理按钮点击> UIAlertView *alertView = [[UIAlertView alloc] init ...

  8. 简述SVN服务器配置和客户端操作

    有关SVN服务器的搭建见:http://www.cnblogs.com/DwyaneTalk/p/3997688.html 搭建好环境之后,服务器端需要完成如下操作: 1.需要新建Repository ...

  9. OpenGL VAO, VBO 使用简介

    参照代码样例: // This function takes in a vertex, color, index and type array // And does the initializati ...

  10. Django 跨域CORS

    现在,前端与后端分处不同的域名,我们需要为后端添加跨域访问的支持. 我们使用CORS来解决后端对跨域访问的支持. 使用django-cors-headers扩展 参考文档https://github. ...