zoukankan      html  css  js  c++  java
  • HDU 3460 Ancient Printer

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3460

    题意:给你n个单词,问你将他们全部输出最少需要多少次操作

    每次可以在当前的末尾添加或删除一个字符或者输出整个字符串

    要使得重复使用的最多很容易就想到字典树

    因为每个字符都是需要打出和删除,那么答案的一部分就是 字典树的节点数*2

    因为每个字符串都需要输出,所以答案还要加上n

    但是最后我们可以保留下最长的一个字符串不需要删除操作

    所以答案还要减去最长的长度

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    using namespace std;
    const int mod=998244353;
    const int N=1e4+5;
    struct trie
    {
        int tot;
        int ch[50*N][26];
        void init()
        {
            tot=0;
            memset(ch[tot],0,sizeof(ch[tot]));
        }
        void add(char *s)
        {
            int now=0;
            int len=strlen(s);
            for(int i=0;i<len;i++)
            {
                int t=s[i]-'a';
                if (!ch[now][t])
                {
                    ch[now][t]=++tot;
                    memset(ch[tot],0,sizeof(ch[tot]));
                }
                now=ch[now][t];
            }
        }
    }tree;
    char a[N][55];
    int main()
    {
        int n;
        int maxx;
        while(scanf("%d",&n)!=EOF)
        {
            tree.init();
            maxx=0;
            for(int i=1;i<=n;i++)
            {
                scanf("%s",a[i]);
                int len=strlen(a[i]);
                maxx=max(maxx,len);
                tree.add(a[i]);
            }
            int ans=tree.tot*2+n-maxx;
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    [ SHOI 2012 ] 随机树
    [ BZOJ 4318 & 3450 / CodeForces 235 B ] OSU!
    [ HNOI 2015 ] 亚瑟王
    [ JSOI 2015 ] Salesman
    [ ZJOI 2007 ] 时态同步
    [ Luogu Contest 10364 ] TG
    [ CodeForces 17 E ] Palisection
    [ BZOJ 2160 ] 拉拉队排练
    Manacher 学习笔记
    [ CodeForces 865 D ] Buy Low Sell High
  • 原文地址:https://www.cnblogs.com/bk-201/p/7506468.html
Copyright © 2011-2022 走看看