hdu 3460 Ancient Printer

 

Ancient Printer

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


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
 

 

Source
 

 

有点像贪心~不过感觉很巧妙

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <string>
 7 #include <cmath>
 8 #include <map>
 9 #include <algorithm>
10 #define N 500015
11 #define INF 1000000
12 #define ll long long
13 using namespace std;
14 string word[100005];
15 int check(string a,string b)
16 {
17     int i = 0;
18     while(i < (int)a.length() && i < (int)b.length() && a[i] == b[i]) {i++;}
19     return a.length() - i + b.length() - i + 1;//删除非共同部分,加上新部分再加上按一下输出
20 }
21 int main(void)
22 {
23     int n,i,sum,maxs;
24     while(scanf("%d",&n) != -1)
25     {
26         for(i = 0; i < n; i++)
27             cin>>word[i];
28         sort(word,word+n);
29         sum = 0;
30         maxs = word[0].length();
31         sum +=  word[0].length() + 1;
32         for(i = 1; i < n; i++)
33         {
34             sum += check(word[i-1],word[i]);
35             if((int)word[i].length() > maxs)
36                 maxs = word[i].length();
37         }
38         sum += word[n-1].length();
39         //先算出所有的单词都敲完并删完所需要的敲击次数
40         
41         
42         sum -= maxs;//最后一个可以不删除,就留下最长的那个
43         printf("%d\n",sum);
44     }
45     return 0;
46 }

 

posted on 2015-08-14 15:03  鱼泪儿  阅读(163)  评论(0编辑  收藏  举报

导航