zoukankan      html  css  js  c++  java
  • Ancient Printer



    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
    2 freeradiant freeopen
     

    Sample Output
    21


    题解:要想得到最少就要操作最少,所以对于有公共前缀的字符串,前缀仅仅打印一次。

    首先我们把全部字符串用字典树保存。此时这棵树的每个节点都要打印,除了根(不包括字母),如今全部节点都要打印和删除。可是最后一个字符串不删除,该字符串肯定是最长的了。所以公式=res=2 * (n (节点数)- 1) + m(打印次数) - maxlen(最长字符串)。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    struct Node
    {
    	Node* next[26];
    	Node()
    	{
    		for(int i = 0;i < 26;i++)
    		{
    			next[i] = NULL;
    		}
    	}
    };
    
    int res = 0;
    Node* root;
    
    int max(int a,int b)
    {
    	return a > b ?

    a : b; } void insert(char* s) { Node* p = root; int len = strlen(s); for(int i = 0; i < len;i++) { int x = s[i] - 'a'; if(p->next[x] == NULL) { Node* q = new Node(); p->next[x] = q; res++; } p = p->next[x]; } } void del(Node*& root) { for(int i = 0; i < 26;i++) { if(root->next[i] != NULL) { del(root->next[i]); } } delete root; } int main() { int n; while(scanf("%d",&n) != EOF) { root = new Node(); char s[100]; res = 0; int maxlen = 0; for(int i = 0;i < n;i++) { scanf("%s",s); maxlen = max(maxlen,strlen(s)); insert(s); } res = 2 * res + n - maxlen; printf("%d ",res); del(root); } return 0; }




  • 相关阅读:
    java里如何使用输入流和输出流实现读取本地文件里内容和写出到本地文件里
    Windows 命令行基础(博主推荐)
    Python2.7编程基础(博主推荐)
    27 个Jupyter Notebook的小提示与技巧
    java里如何实现循环打印出字符或字符数组里的内容
    [转]angularjs之ui-grid 使用详解
    [转]AngularJS 实现 Table的一些操作(示例大于实际)
    [转]js 回车转成TAB(利用tabindex)
    [转] Entity Framework添加记录时获取自增ID值
    [转]使用依赖关系注入在 ASP.NET Core 中编写干净代码
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/7041253.html
Copyright © 2011-2022 走看看