Ultraviolet A -- 1368 (greedy, string simulation)
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
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define from (I, a, n) for (int I = a; I
= A; I --) # define EPS 1e-10 # define mod 1000000007 using namespace std; const double INF = 0x3f3f3f3f; const int MAX = 1000 + 10; int m, n, pos, num; char s [52] [MAX], s1 [MAX], s2 [51]; // s1 is used to record the sequence with the smallest Lexicographic Order that meets the conditions, s2 is used to process the characters int main () {int T; cin> T; 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 (s2, s2 + pos ); // in ascending order, the first character found must be a small lexicographically small character c = s2 [0]; from (j, 1, pos) {if (s2 [j] = s2 [J-1]) cnt ++; else {if (maxlen