zoukankan      html  css  js  c++  java
  • 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.
    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

    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.将缓存区最后面字符删除
    现在我想打印一些单词,问最少要操作几次才能将打印出所有的单词卡片
    输入:
    多组输入,每组输入一个n,代表下面有n个单词
    接着n行每一行有一个单词吗,单词长度最多不超过50
    输出:
    输出把所有的单词卡片打印出来最少的操作数,打印顺序与输入顺序无关。

    注意,打印该单词时,要求现在缓存区内都为该单词。

    这道题思考了好长时间,各种麻烦操作都想了出来。
    最后看了博客…

    大概就是这样的思路,先将所有单词建立一个字典树,采用前序遍历树的方法就可以将所有单词全部打印出来,而且操作最少。
    字典树的每个节点都是一个字母,那么每个字母都有一个添加和删除的操作,这样就可以把所有单词全部打印,又因为最后一个单词不用删除。
    所以总的操作次数=字典树节点的个数*2+单词个数(要打印出来)—maxlen(输入单词的集合)

    所以代码思路:
    1.将所有单词建立一个字典树,并记下节点个数
    2.同时找到单词中的最长单词的长度
    3.Get到答案

    #include<iostream>
    #include<string.h>
    #include<string>
    #include<stdio.h>
    #include<cstdlib>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int maxn=500100;
    struct Node
    {
        int net[26];
        Node()
        {
            clear();
        }
        void clear()
        {
            memset(net,-1,sizeof(net));
        }
    }node[maxn];
    int top;
    int hash_letter(char c)
    {
        return c-'a';
    }
    void clear_tree(int &top)//清空树
    {
        for(int i=0;i<=top;++i)
            node[i].clear();
        top=0;
    }
    void inser_node(char *s)//插入字符串
    {
        int now=0;
        while(*s)
        {
            if(node[now].net[hash_letter(*s)]==-1)
            {
                node[now].net[hash_letter(*s)]=++top;
            }
            now=node[now].net[hash_letter(*s++)];
        }
    }
    int main()
    {
        int n;
        char str[100];
        int maxlen;
        while(~scanf("%d",&n))
        {
            clear_tree(top);//初始化
            maxlen=-1;
            for(int i=0;i<n;++i)
            {
                scanf("%s",str);
                inser_node(str);
                maxlen=max(maxlen,(int)strlen(str));
            }
            printf("%d
    ",2*top-maxlen+n);
        }
    }
    
  • 相关阅读:
    Java ------- 数字转换为 字符串
    电脑连接线 --- DVI,VGA,HDMI
    根据URL 在桌面生成快捷方式
    .net ----------- EXCEL导出为文本格式,
    护照与签证
    jq:选择器的应用(表格的隔行换色、全选和全不选)
    jq:选择器(属性、表单、表单对象属性)
    jq:选择器(基本过滤选择器)
    jq:选择器(层次选择器)
    jq:选择器(基本选择器)
  • 原文地址:https://www.cnblogs.com/dchnzlh/p/10427312.html
Copyright © 2011-2022 走看看