HDOJ 3460 Ancient Printer
来源:互联网 发布:freecodecamp python 编辑:程序博客网 时间:2024/06/04 09:40
Ancient Printer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1935 Accepted Submission(s): 972
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.
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.
Output
For each test case, output one integer, indicating minimum number of operations.
Sample Input
2freeradiantfreeopen
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
结果=字符串数+(字典树节点数-1)*2-最长字符串长度。
#include <bits/stdc++.h>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define f(i,a,b) for(int i=(a);i<=(b);++i)const int maxn = 26;const int mod = 1e9+7;const int INF = 1e9;#define ll long long#define rush() int T;scanf("%d",&T);while(T--)struct Trie{ int next[maxn];};int n,Count,Max;Trie word[500010];void init(){ Count=Max=0; int len=50*n+2; for(int i=0;i<len;i++) for(int j=0;j<maxn;j++) word[i].next[j]=-1;}void createTrie(char *str){ int now=0; int len=strlen(str); if(len>Max) Max=len; for(int i=0;i<len;i++) { int t=str[i]-'a'; if(word[now].next[t]==-1) { word[now].next[t]=++Count;//不能是Count++ now=Count; } else now=word[now].next[t]; }}int main(){ char str[55]; while(~scanf("%d",&n)) { init(); for(int i=0;i<n;i++) { scanf("%s",str); createTrie(str); } printf("%d\n",n+2*Count-Max); } return 0;}
- HDOJ 3460 Ancient Printer
- HDOJ-3460Ancient Printer(Trie)
- HDOJ 3460 Ancient Printer (字典树 + 深度遍历)
- hdu 3460 Ancient Printer
- hdu 3460 Ancient Printer
- HDU 3460 Ancient Printer
- hdu 3460 Ancient Printer
- hdu 3460 Ancient Printer
- HDU 3460 Ancient Printer
- HDU 3460Ancient Printer
- hdu 3460 Ancient Printer
- HDU 3460 Ancient Printer
- 【HDU】 3460 Ancient Printer
- hud 3460 Ancient Printer
- 【trie】hdu 3460 Ancient Printer
- Ancient Printer
- Ancient Printer 字典树 hdu 3460
- hdu - 3460 - Ancient Printer(Trip)
- ACM递推递归练习J 数学黑洞
- SSM(spring + springMVC + mybatis)配置
- java设计模式之模板方法模式
- maven
- 让Fetch()也可以Timeout
- HDOJ 3460 Ancient Printer
- randomAccessFile
- 师--链表的结点插入
- 神经网络在化学中的应用
- 字符串和存储区
- 为什么加了内存条系统会快一些?
- D:\eclipse\WorkspaceOne\ssm-demo2\target\m2e-wtp\web-resources\META-INF\MANIFEST.MF (系统找不到指定的路径。)
- react+redux+react-redux
- F(x) HDU