HDU 3069 Ancient Printer (模拟)

来源:互联网 发布:阿里云1m带宽下载速度 编辑:程序博客网 时间:2024/05/21 10:46

Ancient Printer

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


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
2freeradiantfreeopen
 

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
2010 ACM-ICPC Multi-University Training Contest(3)——Host by WHU 
 

题意:
给你几个字符串,让你一个个打进去输出。
3个操作 打一个字,删一个字,输出单词。最后一个单词不用删。
POINT:
可知要让最长的单词留在最后。那就先默认全删了,然后加回。
排个序,开始模拟。注意输出也要一次操作。

#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;#define  LL long longconst int N = 10004;string s[N];LL len;void doit(string a,string b){    int i;    int la=a.length();    int lb=b.length();    if(a[0]!=b[0])    {        len+=la+lb;        return;    }    for(i=0;i<min(la,lb)&&a[i]==b[i];i++)    {            }    len+=la+lb-2*(i);}int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)        {            cin>>s[i];        }        sort(s+1,s+1+n);        len=0;        len+=s[1].length();        int max=s[1].length();        for(int i=2;i<=n;i++)        {            doit(s[i-1],s[i]);            if(max<s[i].length())            {                max=s[i].length();            }        }        len+=s[n].length();//删去最后一个单词。        len-=max;//减去最长的单词不删去        len+=n;//加上每个单词输出        printf("%lld\n",len);    }}