UVA1610 (greedy)

Source: Internet
Author: User

Test instructions

Give n strings (n is even);

To construct a string so that half of the n string is less than equal to it, and the other half is greater than it;

The string is required to be as small as possible, while the dictionary sequence is small;


Ideas:

Put all the strings in a sequence;

Then take out the middle of the two-string comparison;


Ac


#include <cstdio> #include <cstring> #include <string> #include <algorithm> #include < iostream>using namespace Std;const int N = 1005;string str[n];string res;int n;int Main ()  {while (scanf ("%d",& n) && N) {getchar (); for (int i = 0; i < n; i++) {getline (cin, Str[i]);} Sort (str, str + n); string a = STR[N/2-1];string b = Str[n/2];res = ""; for (int i = 0; I < a.size () && i < b.size (); i++) {if (a[i] = = B[i]) Res + = A[i];else {if (i = = A.size ()-1) {res + = A[i];} else if ((B[i]-a[i] > 1) | | I! = b.size ()-1) {res + = (A[i] + 1);} else {res + = a[i];for (int j = i + 1; j < A.size (); j + +) {if (j = = A.size ()-1) {res + = A[j];break;} if (a[j]! = ' Z ') {res + = (A[j] + 1); Res + = ' Z ';}} Break;}} cout << res << Endl;}}


UVA1610 (greedy)

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.