Ancient Printer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1511 Accepted Submission(s):
748
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.
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.
minimum number of operations.
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
/******************************* Date : 2015-12-09 22:15:29
Author : WQJ (1225234825@qq.com)
Link : http://www.cnblogs.com/a1225234/
Name : ********************************/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#define LL long long
using namespace std;
struct node
{
node* next[];
node()
{
for(int i=;i<;i++)
next[i]=NULL;
}
};
int n;
char a[];
int ans;
node* root;
void insert(char* s,int len)
{
int i,j;
node* p=root;
for(i=;i<len;i++)
{
int pos=s[i]-'a';
if(p->next[pos]==NULL)
{
ans++;
p->next[pos]=new node;
p=p->next[pos];
}
else
p=p->next[pos];
}
return;
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
int i,j;
int Max=;
ans=;
root=new node;
for(i=;i<n;i++)
{
scanf("%s",a);
int len=strlen(a);
Max=max(Max,len);
insert(a,len);
}
printf("%d\n",ans*+n-Max);
}
return ;
}