前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU3460 (字典树)

HDU3460 (字典树)

作者头像
dejavu1zz
发布2020-10-23 15:21:05
2840
发布2020-10-23 15:21:05
举报
文章被收录于专栏:奇妙的算法世界

题意描述

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代码

代码语言:javascript
复制
#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("%d\n",ans);
    }
}
int main(){
    //IOS;
    solve();
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意描述
  • 思路
  • AC代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档