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;
    }
    
  • 相关阅读:
    android高级页面效果集锦
    2018年Android的保活方案效果统计
    程序员如何预估自己的项目开发时间?
    Google开发者大会:你不得不知的Tensorflow小技巧
    练就Java24章真经—你所不知道的工厂方法
    一个完整Java Web项目背后的密码
    怎么捕获和记录SQL Server中发生的死锁
    使用跟踪标志位分析死锁
    通过SQL Server Profiler来监视分析死锁
    SQL Server中的死锁
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8384982.html
Copyright © 2011-2022 走看看