Given the gene sequence of N species, if the longest common substring lengths of the two sequences S1 and S2 meet the requirements of | S1 | * P <Len & | S2 | * P <Len (P indicates the percentage given by the question ), the two species belong to the same category.
Analysis: Count, LCs + and query set
View code
# Include <iostream> # Include <Algorithm> # Include < String > # Include <Vector> Using Namespace STD; Const Int N = 100 +10 ; Int N, F [N]; Double P; Char STR [N] [N]; Void Init (){ For ( Int I = 0 ; I <n; I ++ ) F [I] = I ;} Int Find (Int X ){ If (X = F [x]) Return F [X]; F [x] = Find (F [x]); Return F [X];} Int LCS ( Const Char * Str1, Const Char * Str2 ){ Int Xlen = strlen (str1 ); // Horizontal length Int TMP [N] = { 0 }; // Save the previous row of the matrix Int Arr [N] = { 0 }; // Current row Int Ylen = strlen (str2 ); // Vertical length Int Maxele = 0 ; // Maximum value in a matrix element Int Pos = 0 ; // Maximum number of Matrix Elements in Column For ( Int I = 0 ; I <xlen; I ++ ){ For ( Int J = 0 ; J <ylen; j ++ ) {Arr [J] = 0 ; If (Str1 [I] = Str2 [J]) { If (J = 0 ) Arr [J] = 1 ; Else Arr [J] = TMP [J- 1 ] + 1 ; If (ARR [J]> Maxele) {maxele = Arr [J]; POS = J ;}}} For ( Int J = 0 ; J <ylen; j ++ ) TMP [J] = Arr [J];} Return Maxele ;} Int Main (){ Int CAS = 0 ; While (Scanf ( " % D % lf " , & N, & P) = 2 ) {Init (); For ( Int I = 0 ; I <n; I ++) Scanf ( " % S " , STR [I]); For ( Int I = 0 ; I <n; I ++ ){ Int A = Find (I ); Int Len1 = Strlen (STR [I]); For (Int J = I + 1 ; J <n; j ++ ){ Int B = Find (j ); If (A = B) // If you already belong to the same class, you do not need to ask for it. Continue ; Int Len = LCS (STR [I], STR [J]); Int Len2 =Strlen (STR [J]); If (Len1 * p * 0.01 <Len & len2 * p * 0.01 < Len) f [B] = A ;}} Int Ans = 0 ; For ( Int I = 0 ; I <n; I ++ ){ If (I = Find (I) ans ++ ;} Printf ( " Case % d: \ n % d \ n " , ++ CAS, ANS );} Return 0 ;}