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;
 /*******************************

 Date    : 2015-12-09 22:15:29
Author : WQJ (1225234825@qq.com)
Link : http://www.cnblogs.com/a1225234/
Name : ********************************/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#define LL long long
using namespace std;
struct node
{
node* next[];
node()
{
for(int i=;i<;i++)
next[i]=NULL;
}
};
int n;
char a[];
int ans;
node* root;
void insert(char* s,int len)
{
int i,j;
node* p=root;
for(i=;i<len;i++)
{
int pos=s[i]-'a';
if(p->next[pos]==NULL)
{
ans++;
p->next[pos]=new node;
p=p->next[pos];
}
else
p=p->next[pos];
}
return;
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
int i,j;
int Max=;
ans=;
root=new node;
for(i=;i<n;i++)
{
scanf("%s",a);
int len=strlen(a);
Max=max(Max,len);
insert(a,len);
}
printf("%d\n",ans*+n-Max);
}
return ;
}

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. 搭建MHA环境【1】规划+linux相关的设置

    [1]规划 MHA这套软件包涵两个部分 1.manager :主要负责对MySQL集群状态的检查&在master 库宕机时对故障进行转移. 2.node      :主要包涵状态检查& ...

  2. 【转】sqlmap用户手册

    http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数2.判断可以用那种SQL注入 ...

  3. cf B. Vasily the Bear and Fly

    http://codeforces.com/contest/336/problem/B #include <cstdio> #include <cstring> #includ ...

  4. 【转】linux tree命令以树形结构显示文件目录结构 ---- 不错

    原文网址:http://jingyan.baidu.com/article/acf728fd19c7eff8e510a3eb.html 今天小编来给分享Linux 系统下一个非常有用的命令的使用:tr ...

  5. margin:0 auto 与 text-align:center 的区别(转载)

    摘自:http://www.cnblogs.com/zhwl/p/3529473.html 基本概念: 1.text-align: 属性规定元素中的文本的水平对齐方式;   该属性通过指定行框与哪个点 ...

  6. Php基本语法数据类型操作基础训练

    <?php /* * Created on 2015年12月17日 * * To change the template for this generated file go to * Wind ...

  7. python - 文件

    ''' 模式 描述 r 以读方式打开文件,可读取文件信息. w 以写方式打开文件,可向文件写入信息.如文件存在,则清空该文件,再写入新内容 a 以追加模式打开文件(即一打开文件,文件指针自动移到文件末 ...

  8. android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法

    最近项目开发中用到了android:launchMode="singleTask" 和 onNewIntent(Intent intent)两个特性,现总结一下经验: androi ...

  9. GridView动态构建OrderBy进行排序

    废话不说,直接上例子: 前台代码: <asp:GridView ID="GridView1" runat="server" AllowSorting=&q ...

  10. 我为什么放弃Go语言

    有好几次,当我想起来的时候,总是会问自己:我为什么要放弃Go语言?这个决定是正确的吗?是明智和理性的吗?事实上我一直在认真思考这个问题. 开门见山地说,我当初放弃Go语言(golang),就是由于两个 ...