Ancient Printer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 2249 Accepted Submission(s): 1130
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
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
题意: 给你n 个名字, 现在你要用这个老旧的打印机去打印这些名字,打印机只有三种操作,打一个字符, 删除一个字符 , 打印一个名字 你要求出最小的操作使得打印出来全部的名字。
思路: 用字典树记录出现过的字符并且记录前缀, 就像 freeradiant 就是 11 那么freeopen 就是4 因为free 已经出现过, 所以我们插入所有的名字,然后最后搜索一下所有出现的字符,*2然后+打印名字操作数n- 最长的名字长度(因为最后打印完之后不需要将打印的字符串删除)
代码:
#include<bits/stdc++.h>
using namespace std;
//***********************************************************************************************
const int maxnode = 4000 * 100 + 10;
const int sigma_size = 27;
int vis[maxnode][27];
int sumcnt;
// 字母表为全体小写字母的Trie
struct Trie {
int ch[maxnode][sigma_size];
int val[maxnode];
int num[maxnode];
int sz; // 结点总数
void clear() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); } // 初始时只有一个根结点
int idx(char c) { return c - 'a'; } // 字符c的编号
// 插入字符串s,附加信息为v。注意v必须非0,因为0代表“本结点不是单词结点”
void insert(const char *s, int v) {
int u = 0, n = strlen(s);
for(int i = 0; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) { // 结点不存在
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0; // 中间结点的附加信息为0
num[sz]=0;
ch[u][c] = sz++; // 新建结点
}
u = ch[u][c]; // 往下走
num[u]++;
}
val[u] = v; // 字符串的最后一个字符的附加信息为v
}
// 找字符串s的长度不超过len的前缀
bool find(const char *s, int len) {
int u = 0;
for(int i = 0; i < len; i++) {
if(s[i] == '\0') break;
int c = idx(s[i]);
if(!ch[u][c]) break;
u = ch[u][c];
if(val[u] != 0) return true; // 找到一个前缀
}
return false;
}
};
#define N 55
Trie trie;
int n;
char s[N];
void dfs(int rt)
{
for(int i=0;i<26;i++)
{
if(trie.ch[rt][i]){
sumcnt++;
dfs(trie.ch[rt][i]);
}
}
return ;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
trie.clear();
int maxlen=-1;
for(int i=1;i<=n;i++)
{
scanf("%s",s);
trie.insert(s,-1);// 没有附加信息
int len=strlen(s);
maxlen=max(maxlen,len);
}
sumcnt=0;
dfs(0);
int ans=sumcnt*2+n-maxlen;
cout<<ans<<endl;
}
return 0;
}