Ancient Printer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1511    Accepted Submission(s):
748

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.

 
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.

 
Output
For each test case, output one integer, indicating
minimum number of operations.
 
Sample Input
2
freeradiant
freeopen
 
Sample Output
21

Hint

The 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

题目大意:类似于把这些单词在电脑上都打一遍,输入一个单词后需要删除,最后一个不需要删除,问需要至少敲多少个按键
想到tire树就好写了。
用tire树构造所有单词,构造时申请了多少个node内存就输出了多少个字母,因为还要删除,所以需要乘以2,但我们会把最长的单词放在最后输入,然后省去了删除操作,所以需要加上他的strlen(),然后加上单词个数=num_Print;
  1. /*******************************
  2.  
  3. Date : 2015-12-09 22:15:29
  4. Author : WQJ (1225234825@qq.com)
  5. Link : http://www.cnblogs.com/a1225234/
  6. Name :
  7.  
  8. ********************************/
  9. #include <iostream>
  10. #include <cstdio>
  11. #include <algorithm>
  12. #include <cmath>
  13. #include <cstring>
  14. #include <string>
  15. #include <set>
  16. #include <vector>
  17. #include <queue>
  18. #include <stack>
  19. #define LL long long
  20. using namespace std;
  21. struct node
  22. {
  23. node* next[];
  24. node()
  25. {
  26. for(int i=;i<;i++)
  27. next[i]=NULL;
  28. }
  29. };
  30. int n;
  31. char a[];
  32. int ans;
  33. node* root;
  34. void insert(char* s,int len)
  35. {
  36. int i,j;
  37. node* p=root;
  38. for(i=;i<len;i++)
  39. {
  40. int pos=s[i]-'a';
  41. if(p->next[pos]==NULL)
  42. {
  43. ans++;
  44. p->next[pos]=new node;
  45. p=p->next[pos];
  46. }
  47. else
  48. p=p->next[pos];
  49. }
  50. return;
  51. }
  52. int main()
  53. {
  54. freopen("in.txt","r",stdin);
  55. while(scanf("%d",&n)!=EOF)
  56. {
  57. int i,j;
  58. int Max=;
  59. ans=;
  60. root=new node;
  61. for(i=;i<n;i++)
  62. {
  63. scanf("%s",a);
  64. int len=strlen(a);
  65. Max=max(Max,len);
  66. insert(a,len);
  67. }
  68. printf("%d\n",ans*+n-Max);
  69. }
  70. return ;
  71. }

Ancient Printer(tire树)的更多相关文章

  1. Ancient Printer HDU - 3460 贪心+字典树

    The contest is beginning! While preparing the contest, iSea wanted to print the teams' names separat ...

  2. Ancient Printer[HDU3460]

    Ancient Printer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Tot ...

  3. Codeforces 714C. Sonya and Queries Tire树

    C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...

  4. 中文分词系列(二) 基于双数组Tire树的AC自动机

    秉着能偷懒就偷懒的精神,关于AC自动机本来不想看的,但是HanLp的源码中用户自定义词典的识别是用的AC自动机实现的.唉-没办法,还是看看吧 AC自动机理论 Aho Corasick自动机,简称AC自 ...

  5. 中文分词系列(一) 双数组Tire树(DART)详解

    1 双数组Tire树简介 双数组Tire树是Tire树的升级版,Tire取自英文Retrieval中的一部分,即检索树,又称作字典树或者键树.下面简单介绍一下Tire树. 1.1 Tire树 Trie ...

  6. [数据结构]字典树(Tire树)

    概述: Trie是个简单但实用的数据结构,是一种树形结构,是一种哈希树的变种,相邻节点间的边代表一个字符,这样树的每条分支代表一则子串,而树的叶节点则代表完整的字符串.和普通树不同的地方是,相同的字符 ...

  7. UVa 11732 (Tire树) "strcmp()" Anyone?

    这道题也是卡了挺久的. 给出一个字符串比较的算法,有n个字符串两两比较一次,问一共会有多少次比较. 因为节点会很多,所以Tire树采用了左儿子右兄弟的表示法来节省空间. 假设两个不相等的字符串的最长公 ...

  8. UVa 1401 (Tire树) Remember the Word

    d(i)表示从i开始的后缀即S[i, L-1]的分解方法数,字符串为S[0, L-1] 则有d(i) = sum{ d(i+len(x)) | 单词x是S[i, L-1]的前缀 } 递推边界为d(L) ...

  9. Tire树

    Trie树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种. 典型应用是用于统计和排序大量的字符串(但不仅限于字符串), 所以经常被搜索引擎系统用于文本词频统计. 字典树(Trie)可以保存 ...

随机推荐

  1. js 日常问题记录

    1.解决ie6下css背景图不缓存 try{ document.execCommand('BackgroundImageCache',false,true); }catch(e){} 2.为ajax设 ...

  2. 必须弄懂的495个C语言问题

    1.1 我如何决定使用那种整数类型? 如果需要大数 值(大于32, 767 或小于¡32, 767), 使用long 型.否则, 如果空间很重要(如有大数组或很多结构), 使用short 型.除此之外 ...

  3. 如何在word中写出赏心悦目的代码

    短学期的VHDL终于结束了,虽然代码并不是很难,但是框框条条的规矩很多,也算折腾了一会,最后要写一个技术手册,结题报告类似物.考虑到word毕竟套主题比较方便,所以也就没有用LaTeX写,但是很快就发 ...

  4. 使用StreamReader与StreamWriter进行文本文件读写

    namespace filetest { class FileUtil { public static void WriteFile(string file) { using (FileStream ...

  5. 关于Fragment与Activity的想法

    View,Fragment,Activity,ListView等都会涉及到Layout文件 不要从Layout来考虑,而是从Activity,Fragment,来考虑,Layout只是他们的一个属性 ...

  6. python高级编程(第12章:优化学习)1

    # -*- coding: utf-8 -*-# python:2.x__author__ = 'Administrator'#由于5,6,7,8,9,10,11主要是在包,测试之类的学习所以这边就不 ...

  7. struts2——简单登陆实例

    从今天开始,一起跟 各位聊聊java的三大框架——SSH.先从Struts开始说起,Struts对MVC进行了很好的封装,使用Struts的目的是为了帮助我们减少在 运用MVC设计模型来开发Web应用 ...

  8. Dalvik虚拟机简要介绍和学习计划

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8852432 我们知道,Android应用程序是 ...

  9. StaggeredGridView+universal-image-loader载入网路图片实现瀑布流

    StaggeredGridView 开源lib  https://github.com/maurycyw/StaggeredGridView 文章demo下载地址  http://download.c ...

  10. linux 系统监控系列之vmstat

    vmstat的官方定义是:vmstat - Report virtual memory statistics,即虚拟内存的统计. 先来追根溯源: 什么是虚拟内存? 答:虚拟内存就是磁盘上虚拟出来可以当 ...