Ultraviolet A: 10602-Editor Nottoobad (Greedy)

Source: Internet
Author: User

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;}
  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.