算法:字典树

题意:给你一些单词,有一台打印机只能进行以下三种操作

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的更多相关文章

  1. hdu 3460 Ancient Printer

    Problem Description The contest is beginning! While preparing the contest, iSea wanted to print the ...

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

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

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. POJ3484 Showstopper (二分+字符串处理)

    POJ3484 Showstopper 题目大意: 每次给出三个数x,y,z,用这三个数构成一个等差数列,x为首项,y是末项,z是公差 总共给出n组x,y,z( n待定),求这n组数列中出现次数为奇数 ...

  2. DHTML【2】--HTML

    通过题目,大家已经明确知道,从这一节开始介绍DHTML中的最基础的部分HTML,对于HTML等概念上一节已经做了概述,这一节不再赘余.在学习HTML之前,先告诉大家一个好消息,HTML不难,比C++. ...

  3. MySQL 5.6 root密码丢失,使用mysqld --skip-grant-tables

    MySQL 5.6 root密码丢失,(window平台)使用mysqld –skip-grant-tables启动MySQL服务,出现警告: 1 [Warning] TIMESTAMP with i ...

  4. iOS 7 二维码

    维码扫描 2014-06-13 10:20:29|  分类: iOS|举报|字号 订阅     下载LOFTER客户端     // //  TCTosweepScan.m //  TongCheng ...

  5. Ubuntu常用命令与技巧

    sudo apt-get install 软件名 安装软件命令 sudo nautilus 打开文件(有root权限) su root 切换到“root” ls 列出当前目录文件(不包括隐含文件) l ...

  6. pcduino连接OTG登录远程桌面

    由于没有HDMI的显示屏,为了方便起见,使用了pcduino的OTG来连接到虚拟桌面,可是发现连接上虚拟桌面后,电脑的外网就断了.下面这个方法让你既可以连接到pcduino,又可以让电脑能上外网. 打 ...

  7. NGINX小技巧--将所有目录和目录下所有文件分别给与不同的权限

    为了安全,有时要将文件的权限进行限制,但,目录如果没有755,则不能进入. 所以需要分别给权限 find ./ -type f -name "*" |xargs ls -l

  8. RazorPad中的ModelProvider

    在RazorPad的右侧 我们可以提供模型的结构,Json数据结构体 当提供多个的时候 是Json中的数组 [{     Name: "NI" }, {     Name: &qu ...

  9. JAVA中的时间操作

    java中的时间操作不外乎这四种情况: 1.获取当前时间 2.获取某个时间的某种格式 3.设置时间 4.时间的运算 好,下面就针对这四种情况,一个一个搞定. 一.获取当前时间 有两种方式可以获得,第一 ...

  10. logstash 发送慢页面到zabbix告警

    input { file { type => "zj_frontend_access" path => ["/data01/applog_backup/zjz ...