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执白棋轮流落子 与棋子直线相连的空白交叉点叫做气.当 ...
随机推荐
- gradle下载(转)
http://services.gradle.org/distributions services.gradle.org/ distributions/ gradle-2.2.1-rc-1-all.z ...
- java中间==、equals和hashCode差额
java于==.equals().hashCode()和比较两个对象. 关于== ==是easy理解的.java设计java就是要比較两个对象是不是同一个对象. 对于引用变量而言.比較的时候两个引用变 ...
- wamp无法登录phpmyadmin问题
文章来源:PHP座谈会 地址:http://bbs.phpthinking.com/forum.php? mod=viewthread&tid=163 第一步.用navicat确认一下,自己的 ...
- gif动图快速制作方法(附工具)(转)
现在写博客或是wiki的过程中,会经常引用到图片,特别是客户端经常与页面相关所以截图不可避.但是越来越多的效果仅仅一张图片是无法清楚的描述.并且博客或是wiki也是支持gif图的.gif图的制作方法有 ...
- 移动应用开发(IOS/android等一下)在一般图像缓存方案评述(附流程图)
在移动应用开发.我们经常从网络请求到该设备显示遇到的场景图片. 假设多次发动每个请求,废物流.浪费电.: 将图片持久化到磁盘也不失为一种策略:但每次从文件读取图片也存在一定的io开销,就算採用此策略, ...
- 如何使用junit4写单元测试用例(转)
JUnit4是JUnit框架有史以来的最大改进,其主要目标便是利用Java5的Annotation特性简化测试用例的编写. 先 简单解释一下什么是Annotation,这个单词一般是翻译成元数据.元数 ...
- poj 2408 Anagram Groups(hash)
id=2408" target="_blank" style="">题目链接:poj 2408 Anagram Groups 题目大意:给定若干 ...
- UVA - 12001 UVa Panel Discussion
Description UVa Panel Discussion The UVa online judge team is arranging a panel discussion for the ...
- [原创].NET 分布式架构开发实战之一 故事起源
原文:[原创].NET 分布式架构开发实战之一 故事起源 .NET 分布式架构开发实战之一 故事起源 前言:本系列文章主要讲述一个实实在在的项目开发的过程,主要包含:提出问题,解决问题,架构设计和各个 ...
- 处理FTP上传成功推理
#登录FTPserver获取指定文件 #$1:server住址 #$2:帐户 #$3:password #$4:文件路径名 #$5:本地文件路径名 #$6:本地文件名 #比量ftp上传结果: #获取上 ...