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

 
Author
iSea @ WHU
 
Source
 
 
题解:刚开始没理解题意,看了题解才知道要干嘛.....    知道题意之后,感觉真的挺水的.....

选择最长的那个单词在最后一次打印,这样子才能节省最短的步数。

所以,直接遍历统计Trie树上的字母个数ans, 设最长的单词长度为max,那么答案便是 2*ans+n-max.

ans要乘2, 是因为打印后还要一个一个删除掉,相当于每个单词走了两次。

加上n, 就是打印的次数。

减去maxLen,是因为最后一个单词不需要删除。

代码:

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <queue>
#include <stdlib.h>
using namespace std; struct node
{
int count;
node *next[];
node(){ //构造函数
count=;
memset(next,,sizeof(next));
}
};
node *root;
int ans;
char s[];
void insert(char *s)
{
node *p;
p=root;
int len=strlen(s);
for(int i=;i<len;i++){
int id=s[i]-'a';
if(!p->next[id]){
ans++;
p->next[id]=new node;
}
p=p->next[id];
}
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
root=new node;
ans=;
int max=;
for(i=;i<=n;i++)
{
scanf("%s",s);
insert(s);
int len=strlen(s);
if(len>max) max=len;
}
printf("%d\n",*ans-max+n);
}
return ;
}

hdu 3460 Ancient Printer的更多相关文章

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

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

  2. Ancient Printer[HDU3460]

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

  3. Ancient Printer(tire树)

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

  4. HDU 3839 Ancient Messages(DFS)

    In order to understand early civilizations, archaeologists often study texts written in ancient lang ...

  5. hdu 3839 Ancient Messages (dfs )

    题目大意:给出一幅画,找出里面的象形文字. 要你翻译这幅画,把象形文字按字典序输出. 思路:象形文字有一些特点,分别有0个圈.1个圈.2个圈...5个圈.然后dfs或者bfs,就像油井问题一样,找出在 ...

  6. hdu 3460

    算法:字典树 题意:给你一些单词,有一台打印机只能进行以下三种操作 1.读入 2.删除 3.打印 让你输出最少的操作次数将这些单词全部打印出来: (字典树节点-1)*2  表示读入和删除操作: 打印操 ...

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

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

  8. Ancient Printer

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

  9. HDU 5546 Ancient Go (搜索)

    题意: Alice和Bob正在下古代围棋,规则如下: 棋盘有8×8个格子,棋子下在棋盘的交叉点上,故可以有9×9个落子的位置 Alice执黑棋Bob执白棋轮流落子 与棋子直线相连的空白交叉点叫做气.当 ...

随机推荐

  1. t持久化与集群部署开发详解

    Quartz.net持久化与集群部署开发详解 序言 我前边有几篇文章有介绍过quartz的基本使用语法与类库.但是他的执行计划都是被写在本地的xml文件中.无法做集群部署,我让它看起来脆弱不堪,那是我 ...

  2. PIC16SCM设置不同IO功耗端口状态的影响

    最近做的PIC低功耗微控制器,因此,要设置不同的IO端口状态有关电源的情况测试,在系列万用表的方法来测量电流,供应链管理IO港是在地狱,无头整个系统驱动器.的是PIC16F690单片机. 思路例如以下 ...

  3. [LeetCode129]Sum Root to Leaf Numbers

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

  4. WIZnet通过启动在线培训活动:计算机网络TCP/IP协议而事实上,现在的方法

    为了给大家营造更好的学习环境.WIZnet特此举办第一期培训活动,由WIZnet一线project师为你分享最最前沿和有用的网络技术知识,帮你解答开发过程中的疑问.欢迎前来交流.名额有限(20名满), ...

  5. [LeetCode203]Remove Linked List Elements

    题目: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 ...

  6. PowerDesigner 为sql生成凝视

    在使用pd时发现生成的语句中没有行凝视,这样就得手动来加这些凝视,后来在网上找了一些方法.确实得到了解决,分享一下. 在Database-->edit Current DBMS... 设置dbm ...

  7. 【原创】构建高性能ASP.NET站点之三 细节决定成败

    原文:[原创]构建高性能ASP.NET站点之三 细节决定成败 构建高性能ASP.NET站点之三 细节决定成败 前言:曾经就因为一个小小的疏忽,从而导致了服务器崩溃了,后来才发现:原来就是因为一个循环而 ...

  8. JAVA对数据库进行操作,实现数据库中数据的插入,查询,更改,删除操作

    (—)通过mysql workbench 创建一个数据库,在这里命名为company,然后建一个tb_employee表 (二)以下是java代码对表tb_employee的操作 1 创建一个Empl ...

  9. 多快好省的做个app开发

    从技术经理的角度算一算,如何可以多快好省的做个app [导读]前端时间,一篇“从产品经理的角度算一算,做个app需要多少钱”的文章在网上疯传,可见大家对互联网创业的热情!这次,从一名技术经理的角度再给 ...

  10. stringstream转换CString为string出错

    使用stringstream转换CString为string时,调试时发现是CString赋给stringstream没有问题,stringstram赋给string就不行,倒也不是没有赋成功,只是赋 ...