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;
    }
    
  • 相关阅读:
    hbase 2.0.2 分布式安装配置/jar包替换
    hive character '' not supported here
    request.getSession().getServletContext().getRealPath()的一些坑
    hive 自定义函数
    hive 导出数据的几种方式
    hive 分区表与数据产生关联的三种方式
    hive 日志配置/表头配置
    hive 3.1.0 安装配置
    zookeeper 客户端操作
    linux 循环读取文件的每一行
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8384982.html
Copyright © 2011-2022 走看看