Ancient Printer HDU - 3460 + greedy dictionary tree

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.

InputThere 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.
OutputFor 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

 

Code:

1  / * 
2  The questions mainly greedy, certainly the last to retain the length of the longest string.
3  If the printer is the last remaining one letter and do not, then there is multiplied by the number of nodes created 2 plus n (n print string) operations
 4  last remaining characters may be, it is certainly the last one finished print after the end of the string, that is to say the reduction is finally print out the length of the string
 5  that is certainly the less the greater the length of the string it results
 6  
7  each to create a node should consider removing it after operation , coupled with the string you want to print n. Finally, by subtracting the length of the longest character string like
 . 8  
. 9  * / 
10 #include <the iostream>
 . 11 #include <cstdio>
 12 is #include <CString>
 13 is #include <the cstdlib>
 14 #include <algorithm>
 15  the using  namespace STD ;
 16 typedef Long  Long LL;
 . 17 const int maxn=26;
18 const int mod=998244353;
19 typedef struct Trie* TrieNode;
20 int result;
21 struct Trie
22 {
23     int sum;
24     TrieNode next[maxn];
25     Trie()
26     {
27         sum=0;
28         memset(next,NULL,sizeof(next));
29     }
30 };
31 void inserts(TrieNode root,char s[55])
32 {
33     TrieNode p = root;
34     int len=strlen(s);
35     for(int i=0; i<len; ++i)
36     {
37         int temp=s[i]-'a';
38         if(p->next[temp]==NULL) p->next[temp]=new struct Trie(),result++;
39         p->next[temp]->sum+=1;
40         p=p->next[temp];
41     }
42 }
43 void Del(TrieNode root)
44 {
45     for(int i=0 ; i<2 ; ++i)
46     {
47         if(root->next[i])Del(root->next[i]);
48     }
49     delete(root);
50 }
51 
52 int main()
53 {
54     int n,ans=0,len;
55     char s[55];
56 
57     while(~scanf("%d",&n))
58     {
59         ans=0;
60         TrieNode root = new struct Trie();
61         result=0;
62         for(int i=1; i<=n; ++i)
63         {
64             scanf("%s",s);
65             inserts(root,s);
66             len=strlen(s);
67             if(len>ans)
68             {
69                 ans=len;
70             }
71         }
72         printf("%d\n",result*2+n-ans);
73         Del(root);
74     }
75 
76     return 0;
77 }

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/12001434.html