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 ...
随机推荐
- 玩QQ游戏,见到好几个图像是美女的QQ,就不始玩
玩QQ游戏,见到好几个图像是美女的QQ,光占坑就是不开始玩 加了一个,发现是传播不良网站的QQ 聊天还是自动的 估计是利用webqq写的程序,也就那几句话来回重复,让你去注册网站什么 可以加这个Q去体 ...
- 优先队列实现Huffman编码
首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...
- 【转】linux查看及修改文件权限以及相关
linux查看及修改文件权限以及相关 查看文件权限的语句: 在终端输入: ls -l xxx.xxx (xxx.xxx是文件名) 那么就会出现相类似的信息,主要都是这些: -rw-rw-r-- 一共有 ...
- OC编程之道-接口适配之适配器
已有的类与新的接口之间不兼容的问题相当普遍,人们已为它找到了一个解决方案.这个解决方案就是适配器. 1 何为适配器 what 适配器的主要作用是把被适配者的行为传递给管道另一端的客户端. 将一个类的接 ...
- Python模块之configpraser
Python模块之configpraser 一. configpraser简介 用于处理特定格式的文件,其本质还是利用open来操作文件. 配置文件的格式: 使用"[]"内包含 ...
- StringUtils中 isNotEmpty 和isNotBlank的区别
isNotEmpty : 判断某字符串是否非空 StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = ...
- SQL多表查询,消除表中的重复的内容
看到朋友再写一个SQL语句:两个表a1表中有SN.SN2.TN,b1表有SM.SM2.TN2,若a1的SN中的数据和b1的SM中的数据是一致的,那么将a1中对应的数据修改成b1中对应的数据. upda ...
- iOS Universal Links(通用链接)
公司的运维,发现最近大量的请求 /.well-known/apple-app-site-association这个文件,造成了大量的404,可是这是谁请求的呢? 其实是苹果从iOS9.3开始更改了通用 ...
- jquery mobile
页面:data-role="page" header.content.fooder 过渡:data-transition ="slide" 反向过渡:dat ...
- MySQL延迟复制--percona-toolkit和MASTER TO MASTER_DELAY
为了数据的安全,有的时候数据库需要延迟备份,这里说下两种延迟备份的方法. 一.借助工具. 实现环境: 192.168.189.143 (mysql主库) 192.168.189.144 (mysql备 ...