hdu 3460
算法:字典树
题意:给你一些单词,有一台打印机只能进行以下三种操作
1.读入
2.删除
3.打印
让你输出最少的操作次数将这些单词全部打印出来;
(字典树节点-1)*2 表示读入和删除操作;
打印操作 单词数
最后一个最长的单词不需要进行删除操作;
所以答案=(字典树节点-1)*2+单词数-最长的字符串;
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
代码:
#include<iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stdio.h>
using namespace std;
#define Max 30
struct dot
{
dot *next[Max];
};
dot *newnode()
{
dot *temp=new dot;
for(int i=0;i<Max;i++)
temp->next[i]=NULL;
return temp;
}
void tree(char *st,dot *root,int &k)
{
dot *p=root;
int id=0;
for(int i=0;i<strlen(st);i++)
{
id=st[i]-'a';
if(p->next[id]==NULL)
{
k++; //记录节点数;
p->next[id]=newnode();
}
p=p->next[id];
}
}
void del(dot *t)
{
if(t==NULL) return ;
for(int i=0;i<Max;i++)
if(t->next[i]==NULL)
del(t->next[i]);
delete t;
}
int main()
{
char st[55];
int n,m,i,j,k;
while(cin>>n)
{
dot *root;
root=newnode();
k=0;//没有记录跟节点
m=0;
j=n;
while(n--)
{
cin>>st;
i=strlen(st);
m=max(m,i);
tree(st,root,k);
}
cout<<k*2+j-m<<endl;//没有记录跟节点,所以不需要减一
del(root);
}
return 0;
}
hdu 3460的更多相关文章
- 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 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- PHP echo, print, printf, sprintf函数的区别和使用
1. echo函数: 输出函数,是命令,不能返回值.echo后面可以跟很多个参数,之间用分号隔开,如: echo $myvar1; echo 1,2,$myvar,"<b>bol ...
- linux_grep
grep常用的命令行选项: 选项 说明 -c 只显示有多少行匹配,而不具体显示匹配的行. -h 不显示文件名. -i 在字符串比较的时候忽略大小写. -l 只显示包含匹配模板的行的文件名清单. -L ...
- BIOS讲解
首先 BIOS其实没什么神奇的 就是 Bisic input/output System,所以基本输入输出系统是一块装入了启动和自检程序的EPROM或EEPROM集成块,实际上它是被固化在计算机R ...
- javascript中的一些基本方法收藏
W3C DOM 什么是DOM,DOM其实就是把一个HTML或者XML等符合W3C标准的文档内容模拟成一个JAVA对象,这样才能给JAVA或者JS来操作.下面是JS中模拟出的内置DOM对象documen ...
- jquery幻灯片--渐变
网站上为了设计,需要一些幻灯片效果,现在网站有很多插件可以使用. 想要成为以为牛逼的程序员,绝对不允许只会用别人的插件而已,不然你只能是“代码”的搬运工,而不敢做出自己的创新. 首先使用jquery做 ...
- VIM下Express jade空格问题:expected "indent", but got "newline"
Error: /home/y/my_note/nodejs/myapp/views/index.jade: | -list=[{name:,email:'zhangsan@123.com'}] | - ...
- penetration testers渗透测试,hack,vnc,nat,
penetration testers渗透测试,hack,vnc,nat,
- BZOJ1671: [Usaco2005 Dec]Knights of Ni
1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 175 Solved: 107[Su ...
- 【转】如何过滤 adb logcat 输出
原文网址:http://www.cnblogs.com/imouto/archive/2012/12/11/filtering-adb-logcat-output.html 简介: 本文介绍如何在 s ...
- poj3696:同余方程,欧拉定理
感觉很不错的数学题,可惜又是看了题解才做出来的 题目大意:给定一个数n,找到8888....(x个8)这样的数中,满足能整除n的最小的x,若永远无法整除n 则输出0 做了这个题和后面的poj3358给 ...