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. [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

    In some countries building highways takes a lot of time... Maybe that's because there are many possi ...

  2. Postgres的用户认证

    我们先来讲讲postgresql的用户认证吧. 我想我们有必要明白以下几个问题: 第一.postgresql的用户和操作系统的用户没有任何直接的的关系.虽然在postgaresql的初始安装中,它会有 ...

  3. 学习javascript基础知识系列第三节 - ()()用法

    总目录:通过一段代码学习javascript基础知识系列 注意: 为了便于执行和演示,建议使用chrome浏览器,按F12,然后按Esc(或手动选择)打开console,在console进行执行和演示 ...

  4. redis 的基本语法

    Redis::__construct构造函数 $redis = new Redis(); connect, open 链接redis服务 参数 host: string,服务地址 port: int, ...

  5. 在eclipse中使用svn

    作为一名程序员,svn是比较常用也必然会使用到的一个工具,它的全拼为Subversion,是一个开源的版本控制系统,可以对每次修改的文件和目录进行准确记录,以便在使用的时候及时提取.本文主要介绍如何在 ...

  6. java时间格式转换

    package org.shineway.com; import java.text.ParseException; import java.text.SimpleDateFormat; import ...

  7. Session,有没有必要使用它?

    阅读目录 开始 Session的来龙去脉 Session对并发访问的影响 Session的缺点总结 不使用Session的替代方法 Asp.net MVC 中的Session 现有的代码怎么办? 今天 ...

  8. SQL参数化

    本文来自:caodonglin 一.SQL参数化为什么能防注入? 因为执行计划被重用了,所以可以防SQL注入. 下面有两段SQL     正常SQL: 1 select COUNT(1) from C ...

  9. C++ - 容器(container)的erase()函数

    容器(container)的erase()函数 本文地址: http://blog.csdn.net/caroline_wendy/article/details/23996013 容器(contai ...

  10. java集合总结【转】

    Map.Set.Iterator迭代详解 Map接口定义了四种类型的方法,每个Map都包含这些方法. equals(Object o)比较指定对象与此Map的等价性. hashCode()返回此Map ...