Ancient Printer[HDU3460]
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]的更多相关文章
- Ancient Printer(tire树)
Ancient Printer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- hdu 3460 Ancient Printer
Problem Description The contest is beginning! While preparing the contest, iSea wanted to print the ...
- Ancient Printer HDU - 3460 贪心+字典树
The contest is beginning! While preparing the contest, iSea wanted to print the teams' names separat ...
- 【字母树+贪心】【HDU3460】【Ancient Printer】
题目大意: 一个打印机 只有 打印,删除,a-z.操作 给你一堆队名,如何才能操作次数最少输出全部 (字典树节点数-1)*2 输入,删除操作数 字符串数 printf操作数 最长字符串的长度 最后一个 ...
- Ancient Printer
为找规律题 结果为 节点数*2-最长字段+字段个数 结点不能设置为0 与判断条件相冲突 #include<bits/stdc++.h> using namespace std; ...
- 【英语学习】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 ...
- 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 ...
- 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.
- 紫书例题-Ancient Cipher
Ancient Roman empire had a strong government system with various departments, including a secret ser ...
随机推荐
- linux下vmware的安装、物理分区使用及卸载
1.安装 先下载安装文件VMware-Workstation-Full-12 在命令行下执行下载的文件安装即可(需要root权限) wget https://download3.vmware.com/ ...
- Ajax发送POST请求SpringMVC页面跳转失败
问题描述:因为使用的是SpringMVC框架,所以想使用ModelAndView进行页面跳转.思路是发送POST请求,然后controller层中直接返回相应ModelAndView,但是这种方法不可 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- C#夯实基础之多线程三:线程的优先级
一.为什么需要优先级--线程调度的问题 在现实生活中,优先级是一个很常见的现象:在火车站,如果你是孕妇,你是可以走进站中的专门绿色通道的,可以提前上火车以免拥挤:火警119匪警110出警的时候,都是人 ...
- JAVA基础学习——1.2 环境搭建 之eclipse安装及中文化
安装好jdk,配置好环境变量以后,下面就可以进行安装eclipse了. 闲话少说,eclipse下载地址:http://www.eclipse.org/downloads/ 不大用关注checksum ...
- MVC中使用Entity Framework 基于方法的查询学习笔记 (三)
紧接上文,我们已经学习了MVC数据上下文中两个常用的类,这两个类承载着利用函数方式进行数据查询的全部内容,我们既然已经了解了DbSet<TEntity> 是一个泛型集合,并且实现了一些接口 ...
- Python 2/3 安装与运行环境设置
Python 2/3 安装与运行环境设置: 1.Python 软件源:https://www.python.org/ 下载Win版本 https://www.python.org/downloa ...
- PHP Windows环境部署
1. 说明 本文用来在windows环境下手工搭建PHP开发环境,安装的功能模块主要包含MySQL,PHP以及Apache三个,环境如下: l Window7(64位) l MySQL 5.7.14 ...
- linux 环境变量
电脑中必不可少的就是操作系统.而Linux的发展非常迅速,有赶超微软的趋势.这里介绍Linux的知识,让你学好应用Linux系统.比如要把/etc/apache/bin目录添加到PATH中,方法有三: ...
- 448. Find All Numbers Disappeared in an Array
https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 给出一列数,1 ≤ a[i] ≤ n,n是数组大小,有些 ...