首页 > ACM题库 > HDU-杭电 > HDU 3460-Ancient Printer[解题报告]HOJ
2014
03-30

HDU 3460-Ancient Printer[解题报告]HOJ

Ancient Printer

问题描述 :

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.

输出:

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.

样例输入:

2
freeradiant
freeopen

样例输出:

21
Hint
The sample's operation is: f-r-e-e-o-p-e-n-Print-Del-Del-Del-Del-r-a-d-i-a-n-t-Print

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct str{
 char s[55];
 int l;
};

bool operator <(str a, str b)
{
 if (strcmp(a.s, b.s) < 0)
 return 1;
 return 0;
}

str st[100010];

int main()
{
 int i,j,k;
 int n;
 while (scanf("%d", &n) != EOF)
 {
 for (i = 0;i < n;i++)
 {
 scanf("%s", st[i].s);
 st[i].l = strlen(st[i].s);
 }
 sort(st,st + n);
 int ans = st[0].l;
 int mx = st[0].l;
 for (i = 1;i < n;i++)
 {
 if (st[i].l > mx)
 mx = st[i].l;
 k = min(st[i - 1].l, st[i].l);
 for (j = 0;j < k;j++)
 {
 if (st[i - 1].s[j] != st[i].s[j])
 break;
 }
 ans += st[i].l - j;
 }
 ans = ans * 2 - mx + n;
 printf("%d\n", ans);
 }
 return 0;
}

  1. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。