CSU - 1115 最短的名字(字典树模板题)
2015-06-10 11:22
204 查看
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
在一个奇怪的村子中,很多人的名字都很长,比如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[26]; }*tree; void insert(tree h,char *s) { //每一个节点保存的都是每一个字母出现的次数。 tree p=h; int len=strlen(s); for(int i=0;i<len;i++) { int index=s[i]-'a'; if(p->next[index]!=NULL) { p=p->next[index]; p->count++; } else { tree tem=(tree)calloc(1,sizeof(node)); tem->count=1; p->next[index]=tem; p=tem; } } } int find(tree h) { //查找不为空的结点,加上相应的次数,如果count值大于1,则说明有分支,需继续查找下去 tree p=h; int sum=0,i; for(int i=0;i<26;i++) { if(h->next[i]!=NULL) { p=h->next[i]; sum+=p->count; if(p->count>1) sum+=find(p); } } return sum; } char s[1000]; int main() { //freopen("a.txt","r",stdin); int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); tree head=(tree)calloc(1,sizeof(node)); while(n--) { scanf("%s",s); insert(head,s); } printf("%d\n",find(head)); free(head); } return 0; }
相关文章推荐
- 使用 OAuth2-Server-php 在 Yii 框架上搭建 OAuth2 Server
- Android编译系统环境初始化过程分析
- MyEclipse中配置struts.xml自动提示
- Codechef2015 May - Chef and Strings (后缀自动机)
- 网站测试工具
- mysql数据库调优转载。
- shared_ptr
- IOS自动布局之Autoresizing
- MSM8225 thermal设计
- Mac OS靠谱 AndroidStudio 及 IntelliJ 常用快捷键, 经本地验证!
- GestureDetector类及其用法
- 关于session未能正常关闭造成界面假死的问题
- CocoaPods使用异常及处理方式
- POJ 3069 Saruman's Army
- android custom viewgroups 性能分析
- version:99.0-does-not-exist
- oracle 游标循环中 捕获异常 继续执行
- 移动web开发都会遇到的坑(会持续更新)
- JavaScript中的getTimezoneOffset()方法使用详解
- Ubuntu14.04 安装Firefox flash插件