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 ...
随机推荐
- Mongodb系统管理员权限设置
管理员账号无法执行show dbs .show collections Js代码 { "_id" : ObjectId("52a82bb26cea234c4deb06 ...
- UEFI模式下Win10和Linux双系统
一.准备 用Win自带的磁盘管理或者进PE分出一块空间来. 你必须要有一个U盘,然后使用软碟通或者ImageWriter把iso系统镜像文件烧录进去,这是比较传统的方法,但既然我们UEFI启动,那就根 ...
- Windows Server 2012 R2桌面化详细设置图解
一.任务栏左下角启动服务器管理器,然后进行设置. 1.登录不显示服务器管理器 2.本地服务器,看到右边的IE增强的安全配置,如图所示,关闭两项内容.这样就关闭了IE增强安全提示框. 3.“工具”菜单, ...
- Debian8.3安装flash插件,备用~~~
debian的浏览器iceweasel默认没有安装flash播放器,flash player这坨shit,容易使浏览器崩溃,但看在线视频又不得不用这货. 有两种安装方法: 一.安装压缩包 1.先去官网 ...
- 数据库sql语句规则
sql练习: 创建一个名称为mydb1的数据库. create database mydb1; 查看库 show databases; 创建一个使用utf-8字符集的mydb2数据库. create ...
- iOS10 远程推送代码 以及服务器端代码(.net)
// // AppDelegate.m // MyPushDemo // // Created by justapple on 16/12/25. // Copyright © 2016年 dengq ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
- Mac上搭建Nginx + rtmp
介绍 nginx是非常优秀的开源服务器,用它来做hls或者rtmp流媒体服务器是非常不错的选择,本人在网上整理了安装流程,分享给大家并且作备忘. 安装步骤 1.先安装brew: /usr/bin/ru ...
- 解决eclipse中svn插件总是提示输入密码的问题
一.背景 最近在eclipse中使用svn插件进行远程仓库代码管理时,老是出现提示让输入密码,特别烦人,经过努力,终于解决该问题,拿来和大家分享~ 二.svn插件密码机制以及出现问题的原因分析 当我们 ...
- Web应用性能优化思路
瓶颈是什么? 一条4车道的公路,运行非常顺畅,突然出了点事故,事故车导致某个地方只剩下1车道,然后就开始堵车,因为四辆车同时塞向一个车道里.把这个事故清除了,故障车拖走了,道路会开始恢复了通畅. 这个 ...