UVa 12506 Shortest Names
来源:互联网 发布:太平洋炒股软件下载 编辑:程序博客网 时间:2024/05/17 03:05
题目:uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3950
Description
In a strange village, people have very long names. For example: aaaaa, bbb and abababab. You see, it’s very inconvenient to call a person, so people invented a good way: just call a prefix of the names. For example, if you want to call ‘aaaaa’, you can call ‘aaa’, because no other names start with ‘aaa’. However, you can’t call ‘a’, because two people’s names start with ‘a’. The people in the village are smart enough to always call the shortest possible prefix. It is guaranteed that no name is a prefix of another name (as a result, no two names can be equal). If someone in the village wants to call every person (including himself/herself) in the village exactly once, how many characters will he/she use?
Input
The first line contains T (T ≤ 10), the number of test cases. Each test case begins with a line of one integer n (1 ≤ n ≤ 1000), the number of people in the village. Each of the following n lines contains a string consisting of lowercase letters, representing the name of a person. The sum of lengths of all the names in a test case does not exceed 1,000,000.
Output
For each test case, print the total number of characters needed.
Sample Input
1
3
aaaaa
bbb
abababab
Sample Output
5
题意
每个单词取尽量短的前缀,但只要有若干个单词有相同前缀,它们就要同时再向后取一个字母,直到两两之间没有公共前缀
用字典树记公共前缀,数答案时,每一条链都一直数到有一个结点记录的树值为1为止,因为大于1就说明还有若干单词有公共前缀
遍历的过程ans要一直加上结点记录的数
Source Code
#include <stdio.h>#include <stdlib.h>typedef struct node{ int n; struct node *ch[26];}leaf,*trie;void init(trie tree){ int i; tree->n=0; for(i=0;i<26;i++) tree->ch[i]=NULL;}trie creat(){ trie p=(trie)malloc(sizeof(leaf)); init(p); return p;}void insert(trie tree,char *s){ while(*s) { if(tree->ch[*s-'a']==NULL) tree->ch[*s-'a']=creat(); tree->ch[*s-'a']->n++; tree=tree->ch[*s-'a']; s++; }}void count(trie tree,int *ans){ if(!tree) return; int i; for(i=0;i<26;i++) if(tree->ch[i]) { *ans+=tree->ch[i]->n; if(tree->ch[i]->n>1) count(tree->ch[i],ans); }}void cut(trie tree){ int i; for(i=0;i<26;i++) if(tree->ch[i]) cut(tree->ch[i]); free(tree);}char name[2002];int main(){ int iTom; scanf("%d",&iTom); while(iTom--) { int n,ans=0; trie dict=creat(); scanf("%d",&n); while(n--) { scanf("%s",name); insert(dict,name); } count(dict,&ans); printf("%d\n",ans); cut(dict); } return 0;}
- UVa 12506 Shortest Names
- UVA 12506 Shortest Names
- Uva - 12506 - Shortest Names(Trip)
- Shortest Names UVA
- UVa 11713 - Abstract Names
- uva 12821 Double Shortest Paths
- names
- Data Names
- echo 'names';
- echo 'names';
- About Names
- Meaningful Names
- Palindrome Names
- Killer Names
- HDU6143Killer Names
- Shortest Palindromes
- Shortest Prefixes
- Shortest Path
- Qt中的MDI多对话框程序框架
- 每个程序员都应该了解的内存知识
- 杭电试题简单分类
- MVP+Retrofit+RXAndroid使用详解
- 广场街拍白裙美女
- UVa 12506 Shortest Names
- Adapter优化(一)convertView和ViewHolder机制
- 程序编译的几个阶段
- 【SSH】Session持久化报错
- leetcode记录 Reverse Linked List
- getopts 的坑
- soj4510 dp优化
- cf580b
- Struts2 利用拦截器 interceptor 控制登陆和访问权限