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. 【好程序员笔记分享】——URL解码与编码

    -iOS培训,iOS学习-------型技术博客.期待与您交流!------------ 1.url编码 ios中http请求遇到汉字的时候,需要转化成UTF-8,用到的方法是: NSString * ...

  2. Json序列化与反序列化完整实例

    前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1 ...

  3. AFNetworking实现 断点续传

    用AFNetworking实现断点续传,暂停,继续   AFNetworking断点续传暂停恢复 AFNetworking的版本:platform:ios,'7.0' pod "AFNetw ...

  4. cf B. Simple Molecules

    http://codeforces.com/contest/344/problem/B #include <cstdio> #include <cstring> using n ...

  5. ios 导航栏和旋屏

    1,状态栏(UIStatusBar) http://my.oschina.net/shede333/blog/304560 2,visibleViewController和topViewControl ...

  6. Codeforces 67C Sequence of Balls 编辑距离 dp

    题目链接:点击打开链接 有一个交换操作比較特殊,所以记录每一个点距离自己近期的那个字符的位置 然后交换就相当于把第一行要交换的2个字符 之间的字符都删掉 把第二行要交换的2个字符 之间的字符都插入第一 ...

  7. SQL 练习题

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  8. JS中面向对象的,对象理解、构造函数、原型、原型链

    6.1 理解对象 6.1.1 对象属性类型 ECMS属性有两种类型:数据属性和访问器属性 1 数据属性 [[configurable]] 表示能否通过Delete 删除属性从而从新定义属性,能否修改属 ...

  9. Android Training: 设备管理

    Android 设备管理 Android2.2 通过Android设备管理API提供对企业级应用的支持.设备管理API在系统级别提供了设备管理特性.这些API可以在企业环境下,需要对员工设备进行控制时 ...

  10. Android 定义重名权限问题

    一直以来对android的权限机制就有一个疑问,因为在使用权限时,实际上只需要permission的name这一个标签,而在定义权限时,android是不会检查是否重名的,那么在两个应用定义了重名权限 ...