Ancient Printer

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

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

#include <stdio.h>
#include <string.h>
class Trie {
#define Trie_MAX_Letter_Num 26
public:
Trie * next[Trie_MAX_Letter_Num];
Trie * father;
int cnt, mark;
Trie() {
cnt = ;
memset(next, NULL, sizeof(next));
father = NULL;
mark = ;
}
void reset() {
for (int i = ; i < cnt; i++) {
if (next[i] != NULL) {
next[i]->reset();
}
delete next[i];
}
mark = false;
}
void Insert(char * ptr) {
Trie * root = this;
while (*ptr != '\0') {
if (root->next[(*ptr) - 'a'] == NULL) {
root->next[(*ptr) - 'a'] = new Trie;
(root->next[(*ptr) - 'a'])->father = root;
}
root = (root->next[(*ptr) - 'a']);
ptr++;
}
root->mark++;
}
bool Delete(char * ptr) {
Trie * root = this;
while (*ptr != '\0') {
if (root->next[(*ptr) - 'a'] == NULL) {
return false;
}
root = (root->next[(*ptr) - 'a']);
ptr++;
}
root->mark--;
return true;
}
Trie * Search(char * ptr) {
Trie * root = this;
while (*ptr != '\0') {
if (root->next[(*ptr) - 'a'] == NULL) {
return NULL;
}
root = (root->next[(*ptr) - 'a']);
ptr++;
}
return root;
}
};
Trie * trie;
char str[];
int dfs(Trie * trie) {
int ret = trie->mark;
for (int i = ; i < ; i++) {
if (trie->next[i] == NULL) {
continue;
}
ret = ret + + dfs(trie->next[i]);
}
return ret;
}
int dep(Trie * trie) {
int ret = , tmp;
for (int i = ; i < ; i++) {
if (trie->next[i] == NULL) {
continue;
}
tmp = dep(trie->next[i]);
if (ret < tmp) {
ret = tmp;
}
}
return ret + ;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
trie = new Trie;
for (int i = ; i < n; i++) {
scanf("%s", str);
str[strlen(str)] = '\0';
trie->Insert(str);
}
printf("%d\n", dfs(trie) - dep(trie) + );
trie->reset();
}
return ;
}

Ancient Printer[HDU3460]的更多相关文章

  1. Ancient Printer(tire树)

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

  2. hdu 3460 Ancient Printer

    Problem Description The contest is beginning! While preparing the contest, iSea wanted to print the ...

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

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

  4. 【字母树+贪心】【HDU3460】【Ancient Printer】

    题目大意: 一个打印机 只有 打印,删除,a-z.操作 给你一堆队名,如何才能操作次数最少输出全部 (字典树节点数-1)*2 输入,删除操作数 字符串数 printf操作数 最长字符串的长度 最后一个 ...

  5. Ancient Printer

    为找规律题  结果为   节点数*2-最长字段+字段个数 结点不能设置为0   与判断条件相冲突 #include<bits/stdc++.h> using namespace std; ...

  6. 【英语学习】2016.09.11 Culture Insider: Teacher's Day in ancient China

      Culture Insider: Teacher's Day in ancient China 2016-09-10 CHINADAILY Today is the 32nd Chinese Te ...

  7. Good Bye 2015 D. New Year and Ancient Prophecy

    D. New Year and Ancient Prophecy time limit per test 2.5 seconds memory limit per test 512 megabytes ...

  8. When you install printer in Ubuntu, just need a ppd file.

    Search printing in the system and add printer. Then import ppd file. That is all.

  9. 紫书例题-Ancient Cipher

    Ancient Roman empire had a strong government system with various departments, including a secret ser ...

随机推荐

  1. Sql Server创建函数

    在使用数据库的过程中,往往我们需要对有的数据先进行计算,然后再查询出来,所以我们就需要创建函数来完成这项任务,在数据库的Programmability(如图1)下面的Function中创建函数(如图2 ...

  2. runtime理论知识

    http://southpeak.github.io/2014/10/25/objective-c-runtime-1/ 转载http://www.jianshu.com/p/6b905584f536 ...

  3. 关于datepicker只显示年、月、日的设置

    关键时侯,还得看官方文档.花了半个多小时,找了网上一大堆答复,然后一一验证,90%没有能解决问题. 先给出官方文档的URL: http://bootstrap-datepicker.readthedo ...

  4. Android 获得AndroidManifest文件里自定义的meta标签内容

    try { ApplicationInfo appInfo= this.getPackageManager().getApplicationInfo(getPackageName(),PackageM ...

  5. Jade 模板引擎使用

    在 Express 中调用 jade 模板引擎 jade 变量调用 if 判断 循环 Case 选择 在模板中调用其他语言 可重用的 jade 块 (Mixins) 模板包含 (Includes) 模 ...

  6. extern

    gcc编译器编译程序有四个阶段,预处理.编译.汇编.链接.预处理阶段会将源代码中的包含的头文件如stdio.h编译进来:编译阶段,gcc首先要检查代码的规范性.是否有语法错误等,以确定代码的实际要做的 ...

  7. rsync参数及通信

    rsync 支持:  本机数据 <-------> 远程数据/本地数据 意义:  支持增量拷贝 --> 备份,节省带宽,时间   rsync -avL 一.常用选项  ******* ...

  8. 调用图片按钮的img图片

    今天是我学前端的第12天.早上起床后活动筋骨时看了<JS的基本属性操作>,作业是模拟手机发送短信.文字都能传输到<div>上,就是图片不知道怎么传.折腾了好久才弄清楚,多亏了某 ...

  9. context:component-scan标签的use-default-filters属性的作用以及原理分析

    一.背景 我们在Spring+SpringMVC+Mybatis的集成开发中,经常会遇到事务配置不起作用等问题,那么本文就来分析下出现这种问题可能的原因以及解决方式. 二.分析及原理窥探 1.项目结构 ...

  10. 模拟搭建Web项目的真实运行环境(六)

    今天把Redis和Mongodb的操作整理一下,方便日后自己查看,废话不多说,直接进入主题. 一.Redis 1. 引用StackExchange.Redis.dll 由于ServiceStack.R ...