算法:字典树

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

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. 最近公共祖先:LCA及其用倍增实现 +POJ1986

    Q:为什么我在有些地方看到的是最小公共祖先? A:最小公共祖先是LCA(Least Common Ancestor)的英文直译,最小公共祖先与最近公共祖先只是叫法不同. Q:什么是最近公共祖先(LCA ...

  2. 《图解CSS3》——笔记(二)

    作者:大漠 勘误:http://www.w3cplus.com/book-comment.html 2014年7月15日15:58:11 第二章  CSS3选择器 2.1  认识CSS选择器 2.1. ...

  3. ecshop代码详解之init.php

    在includes/init.php目录下 因为工作原因,需要对ecshop二次开发,顺便记录一下对ecshop源代码的一些分析: 首先是init.php文件,这个文件在ecshop每个页面都会 调用 ...

  4. MVC 4.0 Razor模板引擎 @Html.RenderPartial 与 @Html.RenderAction 区别

    近来在学习MVC 4.0,设置布局全局网页的页脚,使用了Razor语法 @{ Html.RenderPartial("Footer", Model.FooterData); } 但 ...

  5. C语言初学 计算表达式的值 switch的意义

    #include<stdio.h> main() { int a; printf("请输入一个数字\n"); scanf("%d",&a); ...

  6. [POJ] 2239 Selecting Courses(二分图最大匹配)

    题目地址:http://poj.org/problem?id=2239 Li Ming大学选课,每天12节课,每周7天,每种同样的课可能有多节分布在不同天的不同节.问Li Ming最多可以选多少节课. ...

  7. 封装JDBC事务操作,执行存储过程测试

    Oracle数据库端测试环境见:http://www.cnblogs.com/yshyee/p/4392328.html package com.mw.utils; import java.sql.C ...

  8. Content-Disposition的使用方法

    一.作用: 1)希望某类或者某已知MIME类型的文件(比如:*.gif;*txt;*.htm)能够在访问时弹出"文件下载对话框" 2)希望客户端下载时以指定文件名显示 3)希望某文 ...

  9. 在NGINX上配置HTTPS---血的教训--要重启NGINX

    重启,不是重载!!! 是STOP & START 而不是RELOAD!!! 纠结了好几天...(难道有的NGINX上不用重启????) 你妹的,上次也是,,PHP-FPM,将一个PHP的程序连 ...

  10. (Stack)Basic Calculator I && II

    Basic Calculator I Implement a basic calculator to evaluate a simple expression string. The expressi ...