Ancient Printer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 2081 Accepted Submission(s): 1041
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
题意:就是打印全部单词所需要的最少步骤,有三种操作:增加,删除,打印。
思路:找出一个最长的字符串,他是不会被重复打印的,其他单词肯定是先增加再删除的,每个单词结束都要打印,所以可以得到sum*2-maxn+n。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
using namespace std;
typedef struct node
{
struct node *child[26];
}node,*Node;
Node root;
int sum;
void insert(char *s)
{
int i;
int len=strlen(s);
Node next,temp;
next=root;
for(i=0;i<len;i++)
{
if(next->child[s[i]-'a']==NULL)
{
sum++;
temp=new node;
for(int j=0;j<26;j++)
temp->child[j]=NULL;
next->child[s[i]-'a']=temp;
}
next=next->child[s[i]-'a'];
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int i;
root=new node;
for(sum=0,i=0;i<26;i++)
root->child[i]=NULL;
int maxn=0;
for(i=0;i<n;i++)
{
char s[55];
scanf("%s",s);
insert(s);
int len=strlen(s);
maxn=max(maxn,len);
}
cout<<sum*2-maxn+n<<endl;
}
return 0;
}