Click to open link
The problem is a greedy idea of the string simulation problem, the topic given m length of n string, let you ask for a length of n string, so that the string and the M string corresponding to the character of the number and the smallest.
To make the corresponding position a minimum of characters, each character of the string takes precedence over the number of characters that appear in that position, and if the number of times is the same, select a character with a smaller dictionary order.
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 100 0000007using namespace Std;const double inf=0x3f3f3f3f;const int MAX =1000+10;int M,n,pos,num;char S[52][max],s1[max], The S2[51];//S1 is used to record the smallest sequence of qualifying sequences in the dictionary order, and S2 is used to process each position of the character 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];//takes all the characters from position I to sort (s2,s2+pos);//sorted in ascending order, then the first found must be a small dictionary order C =S2[0]; From (J,1,pos) {if (s2[j]==s2[j-1]) cnt++; else {if (maxlen<cnt) {maxlen=cnt;c=s2[j-1];} Only when the characters appear the number of hours to change, otherwise do not change, so as to ensure the most Dictionary order conditions cnt=1; }} if (maxlen<cnt) {maxlen=cnt;c=s2[pos-1];} After jumping out of the loop also to judge once s1[num++]=c; dis+= (M-maxlen); The total number of characters minus the maximum number of occurrences is the contribution of the position character to the total distance} cout<<s1<<endl<<dis<<endl; } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
uva--1368 (greedy, string emulation)