uva 10602 - Editor Nottoobad(贪心)
来源:互联网 发布:淘宝客申请定向计划 编辑:程序博客网 时间:2024/06/08 14:57
Problem C
EDITOR NOTTOOBAD
Company Macrohard has released it’s new version of editor Nottoobad, which can understand a few voice commands. Unfortunately, there are only two voice commands that it can understand – “repeat the last word”, “delete the last symbol”. However, when one uses “repeat the last word” the editor inserts a blank that separates the words. But the company claims that it is possible to type much faster – simply by less number of presses. For example, such a phrase like “this thin thing” requires only 6 presses of the keyboard.
Action
Number
of presses
Content of the document
Press "this"
4
This
Say “repeat the last word”
0
this this
Say “delete the last symbol”
0
this thi
Press "n"
1
this thin
Say “repeat the last word”
0
this thin thin
Press "g"
1
this thin thing
In order to increase the popularity of it’s product the company decided to organize a contest where the winner will be a person who types a given number of words with minimum number of presses. Moreover, the first word must be typed first, and all the others can be typed in arbitrary order. So, if words “apple”, “plum” and “apricote” must be typed, the word “apple” must be typed first, and the words “plum” and “apricote” can be switched. And the most important for you – you are going to take part in the contest and you have a good friend in the company, who told you the word which will be used in the contest. You want be a winner J, so you have to write a program which finds the order of the words, where the number of presses will be minimum.
Input
The first line of the input contains the T (1≤T≤15) the number of test cases. Then T test cases follow. The first line of each test contains a number N (1≤N≤100) – the number of words that must be pressed. Next N lines contain words – sequences of small Latin letters, not longer than 100 symbols. Remember that the first word must be pressed first!
Output
The first line of the output contains number X - the minimum number of presses, which one has to do in order to type all the words using editor Nottoobad. Next N lines contain the words in that minimum order. If there are several solutions, you can output one of them.
Sample Input
Sample Output
3
3
this
thin
thing
4
popcorn
apple
apricote
plum
2
hello
hello
6
this
thin
thing
21
popcorn
plum
apricote
apple
5
hello
hello
Problem source: Russian National Team Olympiad 2000
Problem author: Andrew Stankevich
Problem translation: Dmytro Chernysh
Problem solution: Andrew Stankevich, Dmytro Chernysh
开始想先预处理出任意两个字符串间的最短编辑距离,然后求从起点经过所有顶点的最短距离。转化到这个问题后,不知道要怎么做了。想贪心的每次找离当前点距离最近的点作为下一个点,但发现是错的。通过举了一个反例后,突然想到,其实公共前缀长才是节省步骤的关键。每次尽量把最长的公共前缀保留下来,利用这一点正确的贪心算法就出来了。
#include<cstdio>#include<algorithm>#include<vector>#include<cstring>#include<stack>#include<iostream>#include<queue>using namespace std;const int maxn = 100 + 5;typedef pair<int, string> P;char s[maxn][maxn];int common[maxn][maxn];int vis[maxn];vector<int> ans;int main(){ int t; scanf("%d", &t); while(t--){ int n; scanf("%d", &n); for(int i = 0;i < n;i++) scanf("%s", s[i]); for(int i = 0;i < n;i++){ for(int j = i+1;j < n;j++){ int len = min(strlen(s[i]), strlen(s[j])); int cnt = 0; for(int k = 0;k < len;k++){ if(s[i][k]==s[j][k]){ cnt++; } else break; } common[i][j] = common[j][i] = cnt; } } int step = strlen(s[0]); int now = 0; memset(vis, 0, sizeof vis); ans.clear(); ans.push_back(0); for(int k = 0;k < n-1;k++){ int Max = -1, id; for(int i = 1;i < n;i++){ if(!vis[i] && common[now][i] > Max){ Max = common[now][i]; id = i; } } vis[id] = 1; step += strlen(s[id])-Max; ans.push_back(id); now = id; } printf("%d\n", step); for(int i = 0;i < n;i++){ printf("%s\n", s[ans[i]]); } } return 0;}
- UVA 10602 Editor Nottoobad(贪心)
- uva - 10602 - Editor Nottoobad(贪心)
- uva:10602 - Editor Nottoobad(贪心)
- uva 10602 - Editor Nottoobad(贪心)
- EDITOR NOTTOOBAD - UVa 10602 贪心
- EDITOR NOTTOOBAD uva+贪心
- UVA - 10602 Editor Nottoobad 贪心+暴力
- UVa 10602 - Editor Nottoobad
- UVa 10602 - Editor Nottoobad
- uva 10602 - Editor Nottoobad
- UVA 10602 - Editor Nottoobad
- UVA 10602 - Editor Nottoobad
- uva 10602 Editor Nottoobad
- UVA 10602 EDITOR NOTTOOBAD
- UVa 10602 - Editor Nottoobad
- UVA - 10602 Editor Nottoobad
- UVA - 10602 Editor Nottoobad
- uva 10602 Editor Nottoobad
- windows 32位程序编译成64位
- HDOJ--2795--Billboard【线段树】
- java求1的阶乘到10的阶乘的叠加和
- STL中的next_permutation函数用法
- Android四大基本组件介绍与生命周期
- uva 10602 - Editor Nottoobad(贪心)
- linux0.11 中断和异常
- Objective-C中的作用域静态变量详解
- 用java实现终极密码游戏
- [Python]属性和property内建函数
- hdu 1141 Factstone Benchmark
- HDU 3265 Posters (线段树-扫描线)
- 从点击到呈现 — 详解一次HTTP请求(1)
- usaco3.2.2阶乘