UVa 12506 Shortest Names

news/2024/11/30 18:47:44/

题目: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;
}


http://www.ppmy.cn/news/534218.html

相关文章

oracle登录12506,【案例】Oracle报错ORA-12560 sqlplus登录数据库时报错12560

天萃荷净 运维DBA反映,在sqlplus登录数据库时报错ORA-12560: TNS:protocol adapter error,分析原因为sqlplus版本导致 1.sqlplus登录数据库报ORA-12560 C:\Users\oracleplus>sqlplus / as sysdba SQL*Plus: Release 11.2.0.2.0 Production on Tue Feb 14 23:33:31 2012 Co…

UVA - 12506

Shortest Names 比赛时队友直接上的纯暴力&#xff0c;结果超时&#xff0c;我就想先排一下序&#xff0c;之后每次判断相邻的两个&#xff0c;实时更新每个字符串的喊出长度 #include <cstdio> #include <cstring> #include <iostream> #include <alg…

UVA12506 字典树简单应用

分析&#xff1a;构造字典树深搜 采用刘汝佳的左儿子右兄弟的构树方式&#xff0c;节省了大量的空间。 代码如下&#xff1a; #include <cstdio> #include <cmath> #include <cstring> using namespace std;typedef long long ll; const int maxn 1e610; i…

UVA 12506 Shortest Names

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 …

SpringBoot+JPA整合ShardingShpere实现分表-读写分离

ShardingShpere 分表分库读写分离 ​ ShardingShpere 提供来了根据某个字段分库分表的功能和读写分离。 读写分离 当主服务有写入&#xff08;insert/update/delete&#xff09;语句时&#xff0c;从服务器自动获取。 写入线程从 master 数据库查询查询线程从 salve 数据库…

oracle imp导入dmp文件报12506错误解决办法

通过imp导入数据库dmp文件出错解决办法&#xff1a; IMP-00058: 遇到 ORACLE 错误 12560 ORA-12560: TNS: 协议适配器错误 IMP-00000: 未成功终止导入 遇到这个问题后&#xff0c;很多人会认为是本地服务名的原因&#xff0c;但经过测试&#xff0c;发现服务名是正常的。如果服…

Python知识点复习(一)

问1:f.seek(6, 0)的作用是什么? 答1:将文件对象f的指针从开始的位置偏移到6个字节的位置 拓展: 这是Python中文件操作的一个方法,其中f是一个已经打开的文件对象。该方法的第一个参数表示文件偏移量(offset),即从文件的起始位置开始向后移动的字节数,第二个参数表示偏移…

MySQL - 第7节 - MySQL内置函数

1.日期函数 1.1.常用的日期函数 常用的日期函数如下&#xff1a; 1.2.current_date函数 current_date函数用于获取当前的日期。如下&#xff1a; 1.3.current_time函数 current_time函数用于获取当前的时间。如下&#xff1a; 1.4.current_timestamp函数 current_timestamp函数…