zoukankan      html  css  js  c++  java
  • Hdoj 3460

    原题链接

    描述

    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.

    输入

    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.

    输出

    For each test case, output one integer, indicating minimum number of operations.

    样例输入

    2
    freeradiant
    freeopen

    样例输出

    21

    思路

    字典树。
    最后结果就相当于两倍字典树节点个数减去最长单词数。
    节点个数可以用深搜遍历。

    代码

    #include <bits/stdc++.h>
    #define maxn 900000
    using namespace std;
    
    struct node
    {
    	int a[26];
    };
    
    vector<node> trie;
    int tot, sum;
    
    void create(char st[])
    {
    	int len = strlen(st);
    	int u = 0;
    	for(int i = 0; i < len; i++)
    	{
    		int v = st[i] - 'a';
    		if(trie[u].a[v] == 0)
    		{
    			trie[u].a[v] = ++tot;
    			node pnew; 
    			for(int j = 0; j < 27; j++) pnew.a[j] = 0;
    			trie.push_back(pnew);
    		}
    		u = trie[u].a[v];
    	}
    }
    
    void dfs(int u)
    {
    	for(int i = 0; i < 26; i++)
    		if(trie[u].a[i]) 
    		{
    			sum += 2;
    			dfs(trie[u].a[i]);
    		}
    }
    
    int main()
    {
    	int n; 
    	while(scanf("%d", &n) != EOF)
    	{
    		trie.clear(); tot = 0; sum = n;
    		int max = 0;
    		node pnew; 
    		for(int j = 0; j < 27; j++) pnew.a[j] = 0;
    		trie.push_back(pnew);
    		while(n--)
    		{
    			char st[52]; scanf("%s", st);
    			if(strlen(st) > max) max = strlen(st);
    			create(st);
    		}
    		dfs(0);
    		printf("%d
    ", sum - max);
    	}
    	return 0;
    }
    
  • 相关阅读:
    windows下git识别大小写配置
    Vim命令相关
    【Mac使用系列】【转载】十几个Mac实用工具
    【Mac使用系列】Mac锁屏及快捷键
    【Mac使用系列】常用软件及快捷键
    【Linux】Cent OS 虚拟机开机自启动配置
    【工具】使用markdown写ppt
    【产品管理】当我们在做产品时,以何种方式来表达?
    【区块链】区块链学习要点记录
    【树莓派】盒子常见问题处理办法
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8384982.html
Copyright © 2011-2022 走看看