Minimum Spanning Tree problem.
Give you a set of letter sequences and ask you the most likely evolution, that is, to minimize the cost of connecting all sequences.
The cost of each derivation depends on the number of different letters on two strings.
Then calculate the cost, and then Kruskal calculates the minimum value.
# Include <cstdio> # include <cstring> # include <string> # include <queue> # include <algorithm> # include <queue> # include <map> # include <stack> # include <iostream> # include <list> # include <set> # include <cmath> # define INF 0x7fffffff # define EPS 1e-6using namespace STD; int n, m; int Fa [2001]; struct lx {int U, V, Len;} l [2000001]; int father (int x) {If (X! = Fa [x]) Fa [x] = Father (Fa [x]); Return Fa [X];} bool CMP (lx a, lx B) {return. len <B. len;} Char STR [2001] [8]; int getlen (char * a, char * B) {int ans = 0; For (INT I = 0; I <7; I ++) if (a [I]! = B [I]) ans ++; return ans;} int main () {While (scanf ("% d", & N), n) {for (INT I = 1; I <= N; I ++) {Fa [I] = I; scanf ("% s", STR [I]);} int COT = 0; For (INT I = 1; I <= N; I ++) {for (Int J = I + 1; j <= N; j ++) {L [cot]. U = I; L [cot]. V = J; L [cot ++]. len = getlen (STR [I], STR [J]) ;}} sort (L, L + cot, CMP); int ans = 0; For (INT I = 0; I <cot; I ++) {int Fu = Father (L [I]. u); int FV = Father (L [I]. v); If (Fu = FV) continue; Fa [FV] = Fu; ans + = L [I]. len;} printf ("the highest possible quality is 1/% d. \ n ", ANS );}}