天天看点

HDU 3460Ancient PrinterAncient Printer

Ancient Printer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 1054    Accepted Submission(s): 495

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
   
    
        

Author iSea @ WHU   字典树的题目, 最后一次查找的时候一定要是最长的那个。 1,建字典树,            flag  判断该单词是否取过          coutnt  判断该单词有几个单词经过它 2,查找,                如:

HDU 3460Ancient PrinterAncient Printer
HDU 3460Ancient PrinterAncient Printer

不一定从长到短,但最后一个一定为最长的。。。。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
    bool flag;
    int count;
    node *x[26];
};
node *tree;
char stu[10010][60];
int t,ans,p;

void create(node *&tree)
{
    int i;
    tree=(node*)malloc(sizeof(node));
    tree->count=0;
    tree->flag=false;
    for(i=0;i<26;++i)
        tree->x[i]=NULL;
}
void insertTree(char *stu)
{
    node *f=tree;
    int i,j,len=strlen(stu);
    for(i=0;i<len;++i)
    {
        int p=stu[i]-'a';
        if(f->x[p]==NULL)
        {
            f->x[p]=(node*)malloc(sizeof(node));
            f=f->x[p];
            f->count=1;
            f->flag=false;
            for(j=0;j<26;++j)
                f->x[j]=NULL;
        }
        else
        {
            f=f->x[p];
            f->count++;
        }
    }
}
void searchTree(char *stu,int j)
{
    node *f=tree;
    int i,len=strlen(stu);
    for(i=0;i<len;++i)
    {
        int pp=stu[i]-'a';
        f=f->x[pp];
        if(f->flag==false)
        {
            ans++;
            f->flag=true;
        }
        f->count--;
        if((f->count==0)&&(j!=p))
        {
            ans++;
        }
    }
    ans++;
}
int main()
{
    int i,len=-1;
    while(~scanf("%d",&t))
    {
        getchar();
        len=-1;
        ans=0;
        create(tree);
        for(i=1;i<=t;++i)
         {
             scanf("%s",stu[i]),getchar();
             insertTree(stu[i]);
             int len1=strlen(stu[i]);
             if(len1>len)
             {
                 len=len1;
                 p=i;
             }
         }
         for(i=1;i<=t;++i)
         {
             if(i!=p)
             searchTree(stu[i],i);
         }
         searchTree(stu[p],p);
         printf("%d\n",ans);
    }
    return 0;
}