zoukankan      html  css  js  c++  java
  • Ancient Printer[HDU3460]

    Ancient Printer

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
    Total Submission(s): 1803 Accepted Submission(s): 887


    Problem Description
    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

    #include <stdio.h>
    #include <string.h>
    class Trie {
    #define Trie_MAX_Letter_Num 26
    public:
        Trie * next[Trie_MAX_Letter_Num];
        Trie * father;
        int cnt, mark;
        Trie() {
            cnt = 26;
            memset(next, NULL, sizeof(next));
            father = NULL;
            mark = 0;
        }
        void reset() {
            for (int i = 0; i < cnt; i++) {
                if (next[i] != NULL) {
                    next[i]->reset();
                }
                delete next[i];
            }
            mark = false;
        }
        void Insert(char * ptr) {
            Trie * root = this;
            while (*ptr != '') {
                if (root->next[(*ptr) - 'a'] == NULL) {
                    root->next[(*ptr) - 'a'] = new Trie;
                    (root->next[(*ptr) - 'a'])->father = root;
                }
                root = (root->next[(*ptr) - 'a']);
                ptr++;
            }
            root->mark++;
        }
        bool Delete(char * ptr) {
            Trie * root = this;
            while (*ptr != '') {
                if (root->next[(*ptr) - 'a'] == NULL) {
                    return false;
                }
                root = (root->next[(*ptr) - 'a']);
                ptr++;
            }
            root->mark--;
            return true;
        }
        Trie * Search(char * ptr) {
            Trie * root = this;
            while (*ptr != '') {
                if (root->next[(*ptr) - 'a'] == NULL) {
                    return NULL;
                }
                root = (root->next[(*ptr) - 'a']);
                ptr++;
            }
            return root;
        }
    };
    Trie * trie;
    char str[100];
    int dfs(Trie * trie) {
        int ret = trie->mark;
        for (int i = 0; i < 26; i++) {
            if (trie->next[i] == NULL) {
                continue;
            }
            ret = ret + 2 + dfs(trie->next[i]);
        }
        return ret;
    }
    int dep(Trie * trie) {
        int ret = 0, tmp;
        for (int i = 0; i < 26; i++) {
            if (trie->next[i] == NULL) {
                continue;
            }
            tmp = dep(trie->next[i]);
            if (ret < tmp) {
                ret = tmp;
            }
        }
        return ret + 1;
    }
    int main() {
        int n;
        while (scanf("%d", &n) != EOF) {
            trie = new Trie;
            for (int i = 0; i < n; i++) {
                scanf("%s", str);
                str[strlen(str)] = '';
                trie->Insert(str);
            }
            printf("%d
    ", dfs(trie) - dep(trie) + 1);
            trie->reset();
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Java读写锁(ReentrantReadWriteLock)学习
    水平拆分和垂直拆分理解(未完)
    MySQL 主从复制
    sharding-JDBC 实现读写分离
    Linux查看程序端口占用情况
    sharding-jdbc 实现分表
    MySQL explain
    MySQL的七种join
    MySQL建立高性能索引策略
    Nginx企业级优化
  • 原文地址:https://www.cnblogs.com/dramstadt/p/6224877.html
Copyright © 2011-2022 走看看