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;
    }
    
  • 相关阅读:
    关于阿里云centos 2.6下手机表情输入后无法保存到mysql数据库的问题调研及mysql版本从5.1升级到5.7的全过程纪要
    EXTJS 5 学习笔记2
    EXTJS 5 学习笔记1
    关于java.lang.String理解中的一些难点
    关于centos7中使用rpm方式安装mysql5.7版本后无法使用root登录的问题
    大数据专栏
    过采样中用到的SMOTE算法
    linux后台执行命令:&和nohup
    P,R,F1 等性能度量(二分类、多分类)
    word2vec模型cbow与skip-gram的比较
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8384982.html
Copyright © 2011-2022 走看看