算法:字典树

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

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. 不用标准库实现memmove,借助一个缓冲区temp,即使src和dest所指的内存有重叠也能正确拷贝

    void *mymemmove(void *dest,const void *src,size_t count) { char temp[count]; int i; char *d=(char *) ...

  2. hdu 5188

    it's a  dp  difficult problem 试想如果我们遇见这样一道题,: 有n道题目,每道题有一个得分v和用时t: 我们要得够w分:用时最少  怎么做?? 这是一个裸奔的01背包 如 ...

  3. LLVM对注释的新增支持 @ WWDC 2013

    很久之前我就在想:“我应该按照什么格式写注释,才能像Apple官方API那样按住Option键并点击函数名可以跳出文档说明”,如下图: 我理所当然地认为这个功能应该是根据现有注释的格式来进行排版的,于 ...

  4. mybatis入门,基本案例和xml讲解

    mybatis入门 先举一个入门案例 1)创建一个mybatis-day01这么一个javaweb工程或java工程 2)导入mybatis和mysql/oracle的jar包到/WEB-INF/li ...

  5. JDK + Tomcat 安装配置

    学习Java 开发的第一步就是配置环境,今天第一次配置,把过程记录下以备后用. 一.下载JDK.Tomcat JDK:http://www.oracle.com/technetwork/java/ja ...

  6. php 二维数组按照某value值求出最大值最小值

    //商家的等级信息是一个二维数组,求出最小折扣和最大折扣array(0=>array('levelname'=>'银','dis'=>7.5), 1=>array('level ...

  7. Android再学习-20141111-Android应用的七大件

    Android应用的七大件 应用程序的四大组件: Android的四大组件,使用时需要在程序中注册. Activity: Activity是应用程序的一个界面,可以通过这个界面查看联系人.打电话或者玩 ...

  8. 蚁群算法matlab实现

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下用matlab实现蚁群算法:   %蚂蚁算法test   %用产生的一个圆上的十个点来检验蚂蚁 ...

  9. 使用GCD的dispatch_once创建单例

    使用GCD的dispatch_once创建单例 介绍了创建单例的方式,不过后来发现其实在ios 4.0后有更加简单的方式. 就是使用GCD的功能 代码如下: + (instantClass *)sha ...

  10. iphone 拨打电话的 两种方法-备

    大家想不想在自己的应用中拨打电话呀?打电话可以用openURL:这个API, 如:[[UIApplication sharedApplication] openURL:[NSURL URLWithSt ...