HDU 3460 字典树
来源:互联网 发布:网络语大虾是什么意思 编辑:程序博客网 时间:2024/06/07 18:23
Ancient Printer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Problem Description
The contest is beginning! While preparing the contest, iSea wanted to print the teams' names separately on a single paper.
Unfortunately, what iSea could find was only an ancient printer: so ancient that you can't believe it, it only had three kinds of operations:
● 'a'-'z': twenty-six letters you can type
● 'Del': delete the last letter if it exists
● 'Print': print the word you have typed in the printer
The printer was empty in the beginning, iSea must use the three operations to print all the teams' name, not necessarily in the order in the input. Each time, he can type letters at the end of printer, or delete the last letter, or print the current word. After printing, the letters are stilling in the printer, you may delete some letters to print the next one, but you needn't delete the last word's letters.
iSea wanted to minimize the total number of operations, help him, please.
Unfortunately, what iSea could find was only an ancient printer: so ancient that you can't believe it, it only had three kinds of operations:
● 'a'-'z': twenty-six letters you can type
● 'Del': delete the last letter if it exists
● 'Print': print the word you have typed in the printer
The printer was empty in the beginning, iSea must use the three operations to print all the teams' name, not necessarily in the order in the input. Each time, he can type letters at the end of printer, or delete the last letter, or print the current word. After printing, the letters are stilling in the printer, you may delete some letters to print the next one, but you needn't delete the last word's letters.
iSea wanted to minimize the total number of operations, help him, please.
Input
There are several test cases in the input.
Each test case begin with one integer N (1 ≤ N ≤ 10000), indicating the number of team names.
Then N strings follow, each string only contains lowercases, not empty, and its length is no more than 50.
The input terminates by end of file marker.
Each test case begin with one integer N (1 ≤ N ≤ 10000), indicating the number of team names.
Then N strings follow, each string only contains lowercases, not empty, and its length is no more than 50.
The input terminates by end of file marker.
Output
For each test case, output one integer, indicating minimum number of operations.
Sample Input
2freeradiantfreeopen
Sample Output
21HintThe sample's operation is:f-r-e-e-o-p-e-n-Print-Del-Del-Del-Del-r-a-d-i-a-n-t-Print
题意:给出n个字符串,要求摁最少次数键盘,把他们全输出出来,一次只能输出一个。
题解:建立字典树可发现规律,答案为字典树上的结点*2+n-maxlen 搞一搞就过了
#include<stdio.h>#include<string.h>int ind[500005][26],maxlen,idnow,root,k;int n,ans;char str[10005][55];void init(char str[],int k,int l,int now){int i;if(k>=l)return;if(ind[now][str[k]-'a'])init(str,k+1,l,ind[now][str[k]-'a']);else{ind[now][str[k]-'a']=idnow++;ans+=2;init(str,k+1,l,ind[now][str[k]-'a']);}}int main(){int i;while(~scanf("%d",&n)){memset(ind,0,sizeof(ind));ans=n,idnow=2;maxlen=0;int ll;for(i=1;i<=n;i++){scanf("%s",str[i]);ll=strlen(str[i]);init(str[i],0,ll,0);if(ll>maxlen)maxlen=ll;}printf("%d\n",ans-maxlen);}return 0;}
0 0
- hdu 3460 字典树
- HDU 3460 字典树
- Ancient Printer 字典树 hdu 3460
- 【字典树】hdu 3460 Ancient Printer
- hdu 3460 Ancient Printer (字典树)
- hdu 3460 Ancient Printer 字典树
- HDU - 3460 Ancient Printer(字典树)
- HDU 3460 Ancient Printer (字典树)
- hdu 3460 Ancient Printer trie字典树
- HDU 3460 Ancient Printer(字典树)
- hdu 3460-Ancient Printer(字典树&&贪心)
- hdu 1671 字典树
- hdu 1800 字典树
- hdu 1251(字典树)
- HDU 2846 字典树
- hdu 1251 字典树
- HDU 1251 字典树
- HDU 1671 字典树
- PAT-A 1092. To Buy or Not to Buy
- springboot处理session时间周期
- 关于一条sql语句中子查询的返回值不止一个,且拼接成相关规则的字符串
- c++中malloc,free,new,delete区别和联系
- PAT-A 1093. Count PAT's
- HDU 3460 字典树
- 请问.bash_profile文件在linux的哪里
- Openstack Paste.ini 文件详解
- HDU-4729 树链剖分+主席树
- Java8方法引用(::)
- 狄利克雷卷积及莫比乌斯反演定理
- VS2012使用初体验 VS2012与VS2010的区别说明
- Storm基础
- 基于基因表达谱的肿瘤特征基因提取算法MATLAB源代码