Ancient Printer HDU - 3460 贪心+字典树
时间:2019-12-07
本文章向大家介绍Ancient Printer HDU - 3460 贪心+字典树,主要包括Ancient Printer HDU - 3460 贪心+字典树使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
The contest is beginning! While preparing the contest, iSea wanted to print the teams' names separately on a single paper.
Unfortunately, what iSea could find was only an ancient printer: so ancient that you can't believe it, it only had three kinds of operations:
● 'a'-'z': twenty-six letters you can type
● 'Del': delete the last letter if it exists
● 'Print': print the word you have typed in the printer
The printer was empty in the beginning, iSea must use the three operations to print all the teams' name, not necessarily in the order in the input. Each time, he can type letters at the end of printer, or delete the last letter, or print the current word. After printing, the letters are stilling in the printer, you may delete some letters to print the next one, but you needn't delete the last word's letters.
iSea wanted to minimize the total number of operations, help him, please.
Unfortunately, what iSea could find was only an ancient printer: so ancient that you can't believe it, it only had three kinds of operations:
● 'a'-'z': twenty-six letters you can type
● 'Del': delete the last letter if it exists
● 'Print': print the word you have typed in the printer
The printer was empty in the beginning, iSea must use the three operations to print all the teams' name, not necessarily in the order in the input. Each time, he can type letters at the end of printer, or delete the last letter, or print the current word. After printing, the letters are stilling in the printer, you may delete some letters to print the next one, but you needn't delete the last word's letters.
iSea wanted to minimize the total number of operations, help him, please.
InputThere 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.
OutputFor each test case, output one integer, indicating minimum number of operations.Sample Input
2 freeradiant freeopen
Sample Output
21
Hint
The sample's operation is: f-r-e-e-o-p-e-n-Print-Del-Del-Del-Del-r-a-d-i-a-n-t-Print
代码:
1 /* 2 这一道题主要是贪心,最后保留的肯定是长度最长的字符串。 3 如果是最后打印机中也不要剩余一个字母的话,那么就是有创建的节点个数乘与2再加上n(n个打印字符串)个操作 4 最后可以剩余字符,那肯定是打印完最后一个字符串后就结束,那也就是说减少的就是这个最后打印出字符串的长度 5 那肯定这个字符串长度越大那就结果就越小 6 7 之后每创建一个节点还要考虑删除它的操作,再加上要打印n个字符串。最后减去那个最长字符串长度就好了 8 9 */ 10 #include <iostream> 11 #include <cstdio> 12 #include <cstring> 13 #include <cstdlib> 14 #include <algorithm> 15 using namespace std; 16 typedef long long ll; 17 const int maxn=26; 18 const int mod=998244353; 19 typedef struct Trie* TrieNode; 20 int result; 21 struct Trie 22 { 23 int sum; 24 TrieNode next[maxn]; 25 Trie() 26 { 27 sum=0; 28 memset(next,NULL,sizeof(next)); 29 } 30 }; 31 void inserts(TrieNode root,char s[55]) 32 { 33 TrieNode p = root; 34 int len=strlen(s); 35 for(int i=0; i<len; ++i) 36 { 37 int temp=s[i]-'a'; 38 if(p->next[temp]==NULL) p->next[temp]=new struct Trie(),result++; 39 p->next[temp]->sum+=1; 40 p=p->next[temp]; 41 } 42 } 43 void Del(TrieNode root) 44 { 45 for(int i=0 ; i<2 ; ++i) 46 { 47 if(root->next[i])Del(root->next[i]); 48 } 49 delete(root); 50 } 51 52 int main() 53 { 54 int n,ans=0,len; 55 char s[55]; 56 57 while(~scanf("%d",&n)) 58 { 59 ans=0; 60 TrieNode root = new struct Trie(); 61 result=0; 62 for(int i=1; i<=n; ++i) 63 { 64 scanf("%s",s); 65 inserts(root,s); 66 len=strlen(s); 67 if(len>ans) 68 { 69 ans=len; 70 } 71 } 72 printf("%d\n",result*2+n-ans); 73 Del(root); 74 } 75 76 return 0; 77 }
原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/12001434.html
- objective-C 的内存管理之-引用计数
- CompoundButton.OnCheckedChangeListener与RadioGroup.OnCheckedChangeListener冲突
- Liora Rosin & Golan Levi:在北京驾车看洛杉矶的落日
- iphone/ipad/itouch进入DFU模式最简单的操作办法
- 微信小程序for循环里条件判断
- objective-C 的OOP(下)-类(static)方法、实例方法、overwrite(覆写)、属性(property)
- 你真的会玩SQL吗?删除重复数据且只保留一条
- activity结束之后刷新之前的activity的内容
- 徐扬生:与机器人共建未来社会
- Aerospike在实时竞价广告中的应用
- objective-C 的Hello World!
- objective-C 的代码文件组织
- objective-C 的OOP(上)--类定义、继承及方法调用
- jQuery打造智能提示插件二(可编辑下拉框)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 初识 webpack 原理——自定义插件
- 高性能解决线程饥饿的利器 StampedLock
- 前端应该知道的web调试工具——whistle
- 最强大的 CSS 布局 —— Grid 布局
- SAP Spartacus ProductConnector和ProductService实现
- Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】
- Redis 发布订阅,小功能大用处,真没那么废材!
- R语言实现生物序列的降维比对
- Tomcat NIO(9)-IO线程-Overall流程和关键类
- 有限元平面四边形等差单元python编程
- 动图演示:手撸堆栈的两种实现方法!
- [Go] 实战项目在线客服GO-FLY -在gin框架使用IP识别库转换IP为城市
- leetcode1552题解【二分+贪心】
- 这三年被分布式坑惨了,曝光十大坑
- 为什么删数据也提示空间不足呢?