hdu 3460 Ancient Printer
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.
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.
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
选择最长的那个单词在最后一次打印,这样子才能节省最短的步数。
所以,直接遍历统计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的更多相关文章
- Ancient Printer HDU - 3460 贪心+字典树
The contest is beginning! While preparing the contest, iSea wanted to print the teams' names separat ...
- Ancient Printer[HDU3460]
Ancient Printer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Tot ...
- Ancient Printer(tire树)
Ancient Printer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- HDU 3839 Ancient Messages(DFS)
In order to understand early civilizations, archaeologists often study texts written in ancient lang ...
- hdu 3839 Ancient Messages (dfs )
题目大意:给出一幅画,找出里面的象形文字. 要你翻译这幅画,把象形文字按字典序输出. 思路:象形文字有一些特点,分别有0个圈.1个圈.2个圈...5个圈.然后dfs或者bfs,就像油井问题一样,找出在 ...
- hdu 3460
算法:字典树 题意:给你一些单词,有一台打印机只能进行以下三种操作 1.读入 2.删除 3.打印 让你输出最少的操作次数将这些单词全部打印出来: (字典树节点-1)*2 表示读入和删除操作: 打印操 ...
- 【字母树+贪心】【HDU3460】【Ancient Printer】
题目大意: 一个打印机 只有 打印,删除,a-z.操作 给你一堆队名,如何才能操作次数最少输出全部 (字典树节点数-1)*2 输入,删除操作数 字符串数 printf操作数 最长字符串的长度 最后一个 ...
- Ancient Printer
为找规律题 结果为 节点数*2-最长字段+字段个数 结点不能设置为0 与判断条件相冲突 #include<bits/stdc++.h> using namespace std; ...
- HDU 5546 Ancient Go (搜索)
题意: Alice和Bob正在下古代围棋,规则如下: 棋盘有8×8个格子,棋子下在棋盘的交叉点上,故可以有9×9个落子的位置 Alice执黑棋Bob执白棋轮流落子 与棋子直线相连的空白交叉点叫做气.当 ...
随机推荐
- 认识mongoDB数据库
mongodb中有三元素:数据库,集合,文档,其中“集合”对应关系型数据库中的“表”,“文档”对应“行”. 安装mongoDB: 去官网下载对应系统的mongoDB压缩包,解压后将文件夹重命名为mon ...
- 华为上机题汇总----java
以下华为上机题目都是网上整理得到的,代码都是自己调试过的,由于网上java答案较少,欢迎大家批评指正,也希望对准备华为上机的童鞋们有一点点帮助.在练习的过程中成长,加油!~~ 第1题:输入字 ...
- Singleton模式线程相关的(C\C++)
这种需求的最新发展. 我需要一个静态类,无论地方,我可以在线程中调用它public功能对应的功能已经完成. 这个静态类会调用我初始化给它的一个指针,这个指针是与线程一一相应的: 准确来说这样的模式应该 ...
- 什么是PV,UV。
PV浏览(Page View).该网页访问量,每次页面打开PV统计+1,也刷新. IP接入号码指独立IP接入号码,计算基于独立IP在计算的时间段来计算访问我们的网站1二级IP接入号码. 是否这个计算在 ...
- 【YouVersion】 The Bible 圣经App
[YouVersion] The Bible 圣经 App 今天向大家郑重推荐一款非常棒的圣经App : <The Bible> YouVersion 团队开发的全球下载量和安装数目第 ...
- can't connect to mysql server on localhost <10061>
需要启动MySQL服务.它可以通过两种方式来启动使用MySQL: 1.命令行模式. Win+R,进入cmd然后按Enter键.在命令行形式的输入: net start mysql56 mysql56是 ...
- Android setDisplayOptions 具体的使用说明
Android有几个地方使用位计算.实例Intent Flags,它们的定义View onMeasure(int widthMeasureSpec, int heightMeasureSpec),并且 ...
- 无需Visual Studio,5容易的 - 分为报告
总报告设计,例如RDLC.水晶报表等.,需要安装Visual Studio.由VS提供报表设计界面设计报告,由VS设计报告.NET非常方便开发者,.但对于非开发,安装4G一个VS.并且需要Licens ...
- codeigniter 该脚本在运行300s超时退
直接看代码, file:system/core/CodeIgniter.php /* 102 * -------------------------------------------------- ...
- Andrid 多线程下载
本文转自:http://www.2cto.com/kf/201205/130969.html 本文将介绍在android平台下如何实现多线程下载,大家都知道,android平台使用java做为开发语言 ...