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. 安装sitecore数据库和客户端到本机

    (提前先装好数据库和IIS) 安装教程下载:http://download.csdn.net/detail/qq1162195421/6436799 安装过程省略... 1.安装好之后,会自动在安装路 ...

  2. 用递归调用实现字符串反转(java版)

    写一个函数,输入int型,返回整数逆序后的字符串.如:输入123,返回“321”. 要求必须用递归,不能用全局变量,输入必须是一个参数,必须返回字符串. public static String re ...

  3. Backbone源码学习之extend

    extend函数在backbone大概就20来行代码包括注释,对学习Javascript中"类"的继承很是好的学习资料. 下面先贴出Backbone中的源码,其中涉及到unders ...

  4. HTML页面如何判断是手机访问还是电脑访问

    可以通过js来判断访问设备,代码如下: <script type="text/javascript"> var system ={}; var p = navigato ...

  5. Unity3D 响应摇杆

    if (Input.GetKeyUp(KeyCode.Joystick1Button0)) { Debug.Log("Joystick1Button0"); } if (Input ...

  6. 【Django】--Form组件

    Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 例子: 1.创建Form类 from djan ...

  7. 用 javassist 来修改 class 文件

    import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; public class Test { ...

  8. DOCKER 为新启用的容器配置外网IP

    网卡的配置文件存储在 /etc/sysconfig/network-scripts/ 目录下.每个网卡的详细内容将会以不同的名字存储,比如ifcfg-enp0s3. 让我们看下ifcfg-enp0s3 ...

  9. 51nod1265(判断四个点是否共面)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1265 题意:中文题诶- 思路:假设现有a, b, c, d四 ...

  10. 如何清除Xcode8打印的系统日志

    Xcode升级成8之后,就会发现控制台打印的日志莫名其妙的变得超级多,最关键的是很多都是没有用的东西,而有些有用的东西却淹没在那无任何卵用的里面,在这我就说一下如何关掉这些没有用的日志. 1.直接快捷 ...