hdu - 3460 - Ancient Printer

题意:给出N个由小写字母组成的队名,用一台古老的打印机这些队名打印出来,问最少要敲几次键盘(队名之间不用按输入顺序),这台打印机只能执行以下3种操作:

1.在现有基础上的末尾继续输入小写字母;

2.删除最后一个字母;

3.打印现有串。

(1 <= N <= 10000, 队名长度 <= 50)

——>>组织好Trip,记录总结点数sz,每个结点到根结点的距离,得到距离根结点最远的结点的距离Max,那么答案为2 * sz + N - Max。

 

#include <cstdio>

#include <cstring>

#include <algorithm>



using namespace std;



const int maxw = 50 + 10;

int N;

char name[maxw];



struct node{

    int dis;

    node *next[26];

    node(){

        dis = 0;

        memset(next, 0, sizeof(next));

    }

};



struct Trip{

    node *root;

    int Max;

    int sz;



    Trip(){

        root = new node;

        Max = -1;

        sz = 0;

    }



    int idx(char c){

        return c - 'a';

    }



    void insert(char *s){

        node *t = root;

        int len = strlen(s), i;

        for(i = 0; i < len; i++){

            int c = idx(s[i]);

            if(!t->next[c]){

                t->next[c] = new node;

                sz++;

            }

            t->next[c]->dis = t->dis + 1;

            t = t->next[c];

        }

        Max = max(Max, t->dis);

    }



    void solve(){

        printf("%d\n", 2 * sz + N - Max);

    }

};



int main()

{

    while(scanf("%d", &N) == 1){

        Trip trip;

        for(int i = 0; i < N; i++){

            scanf("%s", name);

            trip.insert(name);

        }

        trip.solve();

    }

    return 0;

}


 

 

你可能感兴趣的:(print)