/*DP[I][K]: = The string consisting of the first i+1 characters, divided into k parts, each part contains the number of words added up the maximum value. Initialize: dp[][] = -0x3f3f3f3f
Note: For Dp[i][k], if (i+1) <k, the value of dp[i][k] does not exist and is set to -0x3f3f3f3f. For example: dp[0][2] = -0x3f3f3f3f dp[i][1] = val[0][i]//NOTE:Val[i][j]: = Intercept the original string "characters from I to J (characters containing I and J positions)" to form a string that returns the number of words it containsState equation: dp[i][k] = max (Dp[i][k], dp[j][k-1]) + Num (j+1, i)) (0<=j<i) Answer: Dp[len-1][k]the difficulty of this problem: Beg Val[i][j]VAL[I][J] = val[i][j-1] + num (j) Num (j): = Traverse all the strings that you want to match strarr[m], from the original string STRs position J forward to find the character, find a first character position FST, so strs[fst,j] The length is exactly equal to strarr[m], if the following conditions are met: 1) i<=fst<=jcorresponding Code: (J-i + 1) >= strarr[m].size ()2) The position of the first character of this string FST is not used when seeking val[i][j-1] and num (j)corresponding Code:!used[j-strarr[m].size () + 1]3) strs[i,j] = = Strarr[m]corresponding code: STRSOURCE.SUBSTR (J-strarr[m].size () + 1, strarr[m].size ()) = = Strarr[m]Then: +1 finally found a few of the first positions to meet these three conditions Fst,num (j) value is just a few. */
1#include <iostream>2#include <cstdlib>3#include <cstdio>4#include <cstddef>5#include <iterator>6#include <algorithm>7#include <string>8#include <locale>9#include <cmath>Ten#include <vector> One#include <cstring> A using namespacestd; - Const intINF =-0x3f3f3f3f; - Const intMAXN = About; the Const intMAXK = -; - - intDP[MAXN][MAXK]; - intVAL[MAXN][MAXN]; + intLen, K, S; - stringstrsource; + stringstrarr[Ten]; A at - voidInival () - { - BOOLUSED[MAXN]; - for(inti =0; i < Len; ++i) - { inmemset (Used,0,sizeof(used)); - for(intj = i; J < Len; ++j) to { + if(I! = j) Val[i][j] = val[i][j-1]; - for(intm =0; M < S; ++m) the { * if((J-i +1>= strarr[m].size ()) &&!used[j-strarr[m].size () +1] $&& (Strsource.substr (j-strarr[m].size () +1, strarr[m].size ()) = =strarr[m]))Panax Notoginseng { -++Val[i][j]; theUsed[j-strarr[m].size () +1] =true; + } A } the } + } - } $ $ voidSolve () - { - for(inti =0; i < Len; ++i) the { -dp[i][1] = val[0][i];Wuyi } the for(inti =0; i < Len; ++i) - { Wu for(intj =0; J < I; ++j) - { About for(intK =2; K <= K; ++k) $ { -Dp[i][k] = max (Dp[i][k], dp[j][k-1] + val[j+1][i]); - intBBB =1; - } A } + } thecout << dp[len-1][K] <<Endl; - $ the the /*cout << "\n\n\n\n" << "val:----------------------------" << Endl; the for (int i = 0; i < len; ++i) the { - For (int j = 0; j < Len; ++j) in { the cout << val[i][j] << ""; the } About cout << Endl; the } the the cout << "\n\n\n\n" << "DP:----------------------------" << Endl; + for (int i = 0; i < len; ++i) - { the For (int j = 1; j <= K; ++j)Bayi { the cout << dp[i][j] << ""; the } - cout << Endl; - }*/ the } the the intMain () the { - #ifdef HOME theFreopen ("inch","R", stdin); the //freopen ("Out", "w", stdout); the #endif94 the intCAs; theCIN >>CAs; the while(cas--)98 { AboutStrsource =""; -Memset (Val,0,sizeof(Val));101 for(inti =0; i < MAXN; ++i)102 {103 for(intj =0; J < Maxk; ++j)104 { theDP[I][J] =INF;106 }107 }108 intp;109CIN >> P >>K; the stringstrtmp;111 for(inti =0; I < P; ++i) the {113CIN >>strtmp; theStrsource + =strtmp; the } theLen =strsource.size ();117CIN >>S;118 for(inti =0; i < S; ++i)119 { -CIN >>Strarr[i];121 }122 inival ();123 Solve ();124 } the 126 #ifdef HOME127Cerr <<"Time Elapsed:"<< clock ()/Clocks_per_sec <<"Ms"<<Endl; - _CrtDumpMemoryLeaks ();129System"Pause"); the #endif131 return 0; the}
Code[vs of the dynamic programming of the division] 1040 count the number of words 2001 NOIP National League Improvement Group