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;
    }
    
  • 相关阅读:
    AE Featureclass 添加字段
    C# AE 打开本地数据
    C# DataTable操作
    IDL + AE + C#
    C#, HashTable
    KMeans Clustering
    C#打开外部程序
    二叉树删除操作(java)
    python小游戏水文
    QQ机器人
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8384982.html
Copyright © 2011-2022 走看看