Ancient Printer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1215 Accepted Submission(s): 597
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.
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.
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
21HintThe 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
画一颗trie树,发现就是树的遍历
//#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/
#define for0(i,j) for(int i=0;i<j;i++)
#define for1(i,j) for(int i=1;i<=j;i++)
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(u,e) for(int _i=head[u];~_i;_i=e[_i].next)
/
int n;
char str[55];
int Maxlen;
int cnt;
const int KIND=26;
struct node
{
node *next[KIND];
void init(){MS(next,0);}
}arr[300010],*root;
int nNode=0;
/
node* newNode()
{
arr[nNode].init();
return &arr[nNode++];
}
void insert(node* o,char *str)
{
for(char *p=str;*p;p++)
{
int son=*p-'a';
if(o->next[son]==NULL) o->next[son]=newNode();
o=o->next[son];
}
}
void dfs(node* o)
{
cnt++;
for0(i,26) if(o->next[i]) dfs(o->next[i]);
}
/
void input()
{
Maxlen=0;
cnt=0;
nNode=0;
MS(arr,0);
root=newNode();
for1(i,n)
{
scanf("%s",str);
insert(root,str);
Maxlen=max(Maxlen,(int)(strlen(str)));
}
}
void solve()
{
///init///
calculate
dfs(root); cnt--;
printf("%d\n",cnt*2+n-Maxlen);
/output/
}
/
int main()
{
#ifndef _TEST
freopen("std.in","r",stdin); freopen("std.out","w",stdout);
#endif
while(scanf("%d",&n)!=EOF)
{
input();
solve();
}
#ifdef _TEST
for(;;);
#endif
return 0;
}