Ultraviolet A-1368 (greedy, string simulation), and ultraviolet A-1368 greedy

Source: Internet
Author: User

Ultraviolet A-1368 (greedy, string simulation), and ultraviolet A-1368 greedy

Click Open Link


This is a string simulation question with a greedy idea. Given m strings with a length of n, you can find a string with a length of n, the number and minimum number of characters corresponding to the m string.

To minimize the number of characters in the corresponding position, each character in the string takes precedence over the characters with more occurrences at the position. If the number of occurrences is the same, the character with a smaller Lexicographic Order is selected.

Code:

# Include <iostream> # include <cstdio> # include <string. h> # include <map> # include <stack> # include <queue> # include <algorithm> # include <math. h >#include <vector >#include <set> # define from (I, a, n) for (int I = a; I <n; I ++) # define refrom (I, n, a) for (int I = n; I> = a; I --) # define EPS 1e-10 # define mod 1000000007 using namespace std; const double INF = 0x3f3f3f; const int MAX = 1000 + 10; int m, n, pos, num; char s [52] [MAX], s1 [MAX], s2 [51]; // s1 is used to record the smallest qualified sequence in the Lexicographic Order, and s2 is used to process the character int main () {int T; cin> T at each position; while (T --) {cin> m> n; int dis = 0; memset (s1, 0, sizeof (s1); num = 0; // getchar (); from (I, 0, m) cin> s [I]; from (I, 0, n) {pos = 0; int maxlen =-INF, cnt = 1; char c; from (j, 0, m) s2 [pos ++] = s [j] [I]; // extract all characters at position I from sort (s2, s2 + pos); // sort all characters in ascending order, then the first character found must be a small lexicographic character c = s2 [0]; from (j, 1, pos) {if (s2 [j] = s2 [J-1]) cnt ++; else {if (maxlen <cnt) {maxlen = cnt; c = s2 [J-1];} // changes only when the number of characters appears in hours, otherwise, the condition cnt = 1;} if (maxlen <cnt) {maxlen = cnt; c = s2 [pos-1];} // judge s1 [num ++] = c; dis ++ = (m-maxlen) after jumping out of the loop ); // The total number of characters minus the maximum number of occurrences is the contribution of the characters in the position to the total distance} cout <s1 <endl <dis <endl ;}return 0 ;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.