Ancient Printer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1429 Accepted Submission(s): 715
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:
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
Source
Recommend
zhouzeyong
题意:
用一台打印机去打印给出的一些名字,打印机只能进行三钟操作:
(1)'a'-'z': 打印小写字母
(2)'Del': 删除最后一个字母(如果存在的话)。
(3)'Print':打印当前字符串
要求的是打印玩所有的名字所用的最少步数。
思路:这个题目首先要看懂题目,不然测试数据都过不了,增加一个字母,删除一个字母,打印,每一种都算是一步。
首先建一棵树,将所有字母存到节点上,也就是字典树存下所有的名字,ans =(字典树节点数 - 没有字母的根节点)* 2 + 名字数 - 最长的名字 。
用代码表示就是:ans = (rt - 1)* 2 + n - Maxlen 。
用一台打印机去打印给出的一些名字,打印机只能进行三钟操作:
(1)'a'-'z': 打印小写字母
(2)'Del': 删除最后一个字母(如果存在的话)。
(3)'Print':打印当前字符串
要求的是打印玩所有的名字所用的最少步数。
思路:这个题目首先要看懂题目,不然测试数据都过不了,增加一个字母,删除一个字母,打印,每一种都算是一步。
首先建一棵树,将所有字母存到节点上,也就是字典树存下所有的名字,ans =(字典树节点数 - 没有字母的根节点)* 2 + 名字数 - 最长的名字 。
用代码表示就是:ans = (rt - 1)* 2 + n - Maxlen 。
AC代码如下:
/*
Author:ZXPxx
Memory: 54452 KB Time: 46 MS
Language: C++ Result: Accepted
*/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int maxn = 5e5 + 5;
const int inf = 0x3f3f3f3f;
struct node {
int num;
int next[26];
node() {
memset(next, 0, sizeof(next));
}
} fuck[maxn];
int rt;
int trie_node() {
fuck[++rt] = node();
return rt;
}
void trie_add(int root, char*S) {
int L = strlen(S);
int p = root;
for(int i = 0; i < L; i++) {
int id = S[i] - 'a';
if(!fuck[p].next[id]) {
fuck[p].next[id] = trie_node();
}
p = fuck[p].next[id];
}
fuck[p].num++;
}
int main() {
int n;
char s[100];
while(~scanf("%d", &n)) {
rt = 0;
int Maxlen = 0;
int root = trie_node();
for(int i = 1; i <= n; i++) {
scanf("%s", s);
Maxlen = max(Maxlen, (int)strlen(s));
trie_add(root, s);
}
printf("%d\n", n + (rt - 1) * 2 - Maxlen);
}
return 0;
}
这题貌似还可以用string做,但是本渣渣太弱,还没搞懂,看了kuangbin巨巨的代码,感觉好屌的样子
//kuangbin大神代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
string w[10005];
int calc(string a,string b)
{
int i;
for(i=0;i<a.length()&&i<b.length()&&a[i]==b[i];i++);
return a.length()-i+b.length()-i;
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++) cin>>w[i];
sort(w,w+n);
int sum=w[0].length()+1;
int MAX=w[0].length();
for(i=1;i<n;i++)
{
sum+=calc(w[i],w[i-1])+1;
if(w[i].length()>MAX)
MAX=w[i].length();
}
printf("%d\n",sum+w[n-1].length()-MAX);
}
return 0;
}
亲测:
Memory: 2264 KB
Time: 31 MS 膜拜……~\(^o^)/~