hud 3460 Ancient Printer

来源:互联网 发布:中山入学积分怎么算法 编辑:程序博客网 时间:2024/06/03 22:32

http://acm.hdu.edu.cn/showproblem.php?pid=3460
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 <bits/stdc++.h>using namespace std;struct Trie{    int is_str;    int cnt;    Trie *child[26];    Trie()    {        cnt = 0;        is_str = 0;        for(int i=0; i<26; i++)            child[i] = NULL;    }};Trie *root,*current,*temp;int n,ans,k;void insert(string str){    current = root;    for(int i=0; i<str.length(); i++)    {        if(current->child[str[i]-'a'] == NULL)        {            temp = new Trie;            current->child[str[i]-'a'] = temp;            current = temp;            current->cnt = 1;        }        else        {            current = current->child[str[i]-'a'];            current->cnt++;        }    }}void search(string str,int t){    current = root;    for(int i=0; i<str.length(); i++)    {        current = current->child[str[i]-'a'];        if(current->is_str==0)///如果这个点没有被走过就需要走一次        {            ans++;            current->is_str = 1;        }        current->cnt--;//被覆盖次数也要减一        if(current->cnt==0&&t!=k)///当这个点被覆盖的次数为0时,如果这个节点不是最后一次搜索的,因为最后要删除就还需要再加一次        {            ans++;        }    }}int main(){    while(~scanf("%d",&n))    {        int Max_len = -1;        string str[10005];        root = new Trie;        ans = n;///每个单词倒要打印出来,所以最开始要给按下次数赋值为n        for(int i=0; i<n; i++)        {            cin>>str[i];            insert(str[i]);            int len = str[i].length();            if(len > Max_len)            {                Max_len = str[i].length();                k = i;//cout<<k<<endl;            }        }        for(int i=0; i<n; i++)            if(i!=k)                search(str[i],i);        search(str[k],k);        printf("%d\n",ans);    }    return 0;}
原创粉丝点击