HDU3460 (字典树)
题意描述
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.
可以进行3个操作,打印a~z,删除字母,输出。给定n个字符串,求能够输出n个字符串能够进行的最少操作
思路
我们可以建一颗trie树来保存所有字母,由于相同的前缀只需要打印1次,也只需要删除一次,要想操作次数最少,肯定要把最长的字符串留在最后,所以可以得到公式ans=(node.size())*2-MAXLEN+n。
AC代码
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=500010;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
int cnt=0;
struct trie{
int tot,root,child[N][26];
bool flag[N];
void init(){
memset(child[1],0,sizeof child[1]);
flag[1]=false;
root=tot=1;
}
void Insert(const char*str){
int *cur=&root;
for(const char *p=str;*p;p++){
cur=&child[*cur][*p-'a'];
if(*cur==0){
*cur=++tot;
memset(child[tot],0,sizeof child[tot]);
flag[tot]=false;
cnt++;
}
}
flag[*cur]=true;
}
bool query(const char*str){
int *cur=&root;
for(const char*p=str;*p;p++){
cur=&child[*cur][*p-'a'];
}
return (*cur && flag[*cur]);
}
};
void solve(){
int n;
while(scanf("%d",&n)!=EOF){
trie tr;
cnt=0;
tr.init();
int MAX=-1;
string s;
for(int i=0;i<n;i++){
cin>>s;
int nn=s.size();
MAX=max(MAX,nn);
tr.Insert(s.c_str());
}
int ans=cnt*2-MAX+n;
printf("%dn",ans);
}
}
int main(){
//IOS;
solve();
return 0;
}
- 关于索引的使用模式(r3笔记56天)
- 关于oracle中的半连接(r3笔记55天)
- 关于正则表达式第三篇(r3笔记第52天)
- 关于正则表达式第四篇(r3笔记第53天)
- 外部表简单总结(r3笔记第51天)
- 通过shell脚本监控sql执行频率(r3笔记第50天)
- 和Null有关的函数(r3笔记第48天)
- 关于查询转换的一些简单分析(二) (r3笔记第68天)
- 跨网络拷贝文件的简单实践(r3笔记第67天)
- 关于enq: TX - allocate ITL entry的问题分析(r3笔记第66天)
- Tensorflow学习:使用Tensorflow搭建深层网络分类器
- 关于interval partitioning(r3笔记65天)
- Spark Tips4: Kafka的Consumer Group及其在Spark Streaming中的“异动”(更新)
- 关于数据库中的一些name(r3笔记第64天)
- 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 数组属性和方法
- android如何设置Activity背景色为透明色
- Android 手机防止休眠的两种实现方法
- Android编程实现的首页左右滑动切换功能示例
- 零基础学小程序004----小程序post请求,提交数据到服务器,小程序下单,小程序用户注册功能
- HorizontalScrollView水平滚动控件使用方法详解
- Android Activity生命周期和堆栈管理的详解
- Android学习之SharedPerference存储详解
- Android编程实现对话框Dialog背景透明功能示例
- Android开发中DatePicker日期与时间控件实例代码
- Android Shader应用开发之霓虹闪烁文字效果
- Android中ToggleButton开关状态按钮控件使用方法详解
- 零基础学小程序005---小程序登陆注册功能实现
- Android RecyclerView 复用错乱通用解法详解
- Android把Bitmap保存为PNG图像文件的简单代码
- 零基础学小程序006---小程序获取用户信息用户昵称