Ultraviolet A: 10602-Editor Nottoobad (Greedy)
Question: 10602-Editor Nottoobad
There is a machine that consists of press and copy and delete characters. If you want to input a string, you must specify the minimum number of press actions.
Solution: sort the strings according to the ascall code so that the similarity between the two strings is relatively high. Then, compare the Next string with the previous string to see how many identical copies are available. Just count the number of different characters. This question is confusing because the question has always said that the first string must execute the press action first, but the output can be any one, it is not necessarily the case where the first string is located.
Code:
#include
#include #include
using namespace std;const int N = 105;//char first[N];struct WORDS{char str[N];}words[N];int cmp (const WORDS & w1, const WORDS & w2) {return strcmp(w1.str, w2.str) < 0 ? true :false;}int caculate (int n) {int sum = 0;int len;int k;for (int i = 1; i < n; i++) {len = strlen(words[i].str);k = 0;while (words[i - 1].str[k] && words[i - 1].str[k] == words[i].str[k]) {k++;}sum += len - k;}return sum + strlen (words[0].str);}int main () {int t;scanf ("%d", &t);while (t--) {int n;scanf ("%d", &n);for (int i = 0; i < n; i++)scanf ("%s", words[i].str);//memcpy (first, words[0].str, sizeof (first));sort (words, words + n, cmp);printf ("%d\n", caculate(n));for (int i = 0; i < n; i++)printf("%s\n", words[i].str);}return 0;}