算法:字典树

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

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. NOIP201504推销员

    #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...

  2. ON UPDATE CURRENT_TIMESTAMP

    CREATE TABLE time1 (   id SMALLINT,   time1 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TI ...

  3. ecmascript 的一些发展新动向

    ========== ecmascript 的一些发展新动向 (e5a57b27 - initial commit) 更弱.更受限 严格模式禁止 arguments.callee - 可以 " ...

  4. 3步学会用gulp

    1.安装gulp 安装gulp到全局:npm install -g gulp 安装gulp到某个项目:npm install --save gulp 注意:请先安装nodejs(自带npm) 2.创建 ...

  5. PHP页面中文乱码问题

    首先纯html页要用meta标签声明编码<meta http-equiv="Content-Type" content="text/html; charset=&q ...

  6. About abstract class.

    Abstract means should be realized. Virtual means could be overrided. It is very different!

  7. Python学习笔记总结(四)异常处理

    1.基础 try/except/else:[else是可选的]捕捉由代码中的异常并恢复,匹配except里面的错误,并执行except中定义的代码,后继续执行程序(发生异常后,由except捕捉到异常 ...

  8. 关于DDMS查看Data文件夹

    真机是无法查看的,只有通过模拟器查看

  9. altium designer14的Import wizard 为空的解决方法

    1.首先将安装盘放到光驱里面,如果是虚拟光驱安装,请运行iso文件. 2. 点击DXP-->>Externtion and updates 3. 出现下列界面,选择右面的configure ...

  10. Keil中LIB库的作用、生成与调用

    LIB库有什么用,一个简单的例子就是Silicon Labs为C8051F单片机USB提供的USBXpress LIB库了,如USB发送数据.接收数据等,都是通用性很强的函数,但因为保密的原因,这个函 ...