Ancient Printer(tire树)

Ancient Printer

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


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
题目大意:类似于把这些单词在电脑上都打一遍,输入一个单词后需要删除,最后一个不需要删除,问需要至少敲多少个按键
想到tire树就好写了。
用tire树构造所有单词,构造时申请了多少个node内存就输出了多少个字母,因为还要删除,所以需要乘以2,但我们会把最长的单词放在最后输入,然后省去了删除操作,所以需要加上他的strlen(),然后加上单词个数=num_Print;
 1 /*******************************
 2 
 3 Date    : 2015-12-09 22:15:29
 4 Author  : WQJ (1225234825@qq.com)
 5 Link    : http://www.cnblogs.com/a1225234/
 6 Name     : 
 7 
 8 ********************************/
 9 #include <iostream>
10 #include <cstdio>
11 #include <algorithm>
12 #include <cmath>
13 #include <cstring>
14 #include <string>
15 #include <set>
16 #include <vector>
17 #include <queue>
18 #include <stack>
19 #define LL long long
20 using namespace std;
21 struct node
22 {
23     node* next[26];
24     node()
25     {
26         for(int i=0;i<26;i++)
27             next[i]=NULL;
28     }
29 };
30 int n;
31 char a[110];
32 int ans;
33 node* root;
34 void insert(char* s,int len)
35 {
36     int i,j;
37     node* p=root;
38     for(i=0;i<len;i++)
39     {
40         int pos=s[i]-'a';
41         if(p->next[pos]==NULL)
42         {
43             ans++;
44             p->next[pos]=new node;
45             p=p->next[pos];
46         }
47         else
48             p=p->next[pos];
49     }
50     return;
51 }
52 int main()
53 {    
54     freopen("in.txt","r",stdin);
55     while(scanf("%d",&n)!=EOF)
56     {
57         int i,j;
58         int Max=0;
59         ans=0;
60         root=new node;
61         for(i=0;i<n;i++)
62         {
63             scanf("%s",a);
64             int len=strlen(a);
65             Max=max(Max,len);
66             insert(a,len);
67         }
68         printf("%d\n",ans*2+n-Max);
69     }
70     return 0;
71 }

 

posted @ 2015-12-09 22:43  御心飞行  阅读(278)  评论(0编辑  收藏  举报