Ancient Printer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1922 Accepted Submission(s): 960
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
Author
iSea @ WHU
分析:拥有公共前缀的字符串肯定是集中打印的,这个消息告诉我们什么呢?也就是前缀只要被输入一次(比如样例提示)。如果用字符串建树,不难发现其实需要输入(不是打印)的其实就是树里面的节点。一共需要哪些操作呢?我们来进行始终态分析,最后打印最长字符串肯定是最优解。在最优解情况下——
输入操作:字典树的节点数。(出现过的字母至少要输入一次吧)
删除操作:字典树的节点数-最长字符串长度。(因为最后屏幕打印的最长字符串没有删除)
打印操作:字符串的数量。(无需解释)
求和就行了。
由于我的数组字符串中T[0]作为根节点,tot作为T数组索引,最后tot停留的是新节点的位置(即使还没建立),所以实际存在节点数为 tot-1。
我的代码:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int next[26];
void init()
{
memset(next,-1,sizeof(next));
}
}T[1000000];
int tot,n,maxLen;
char s[100];
void insert(char* s)
{
int i,p=0,len=strlen(s);
maxLen=max(maxLen,len);
for(i=0;i<len;i++)
{
int x=s[i]-'a';
if(T[p].next[x]==-1)
{
T[tot].init();
T[p].next[x]=tot++;
}
p=T[p].next[x];
}
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<26;i++) T[0].next[i]=-1;
tot=1,maxLen=0;
for(int i=0;i<n;i++)
{
scanf("%s",s);
insert(s);
}
printf("%d\n",(tot-1)*2+n-maxLen);
}
return 0;
}
指针字典树写法:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{node *next[26];};
node *root;
int Maxsum;
void Insert(char *s)
{
node *p = root;
for(int i=0; s[i]; i++)
{
int k = s[i] - 'a';
if(!p->next[k])
{
p->next[k] = new node();
Maxsum += 2;
}
p = p->next[k];
}
}
void Free(node *p)
{
for(int i=0; i<26; i++)
if(p->next[i])Free(p->next[i]);
free(p);
}
int main()
{
int N;
while(scanf("%d",&N)!=EOF)
{
char s[100];
int i,MaxLen = 0;
root = new node();
Maxsum = N;
for(i=0; i<N; i++)
{
scanf("%s",s);
Insert(s);
int len = strlen(s);
MaxLen = max(MaxLen, len);
}
printf("%d\n", Maxsum - MaxLen);
Free(root);
}
return 0;
}