DP。注意曾经把赋值写成了==,结果出错半天。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
bool
isInterleaving(string &str1, string &str2, string &str3) { int
n = str1.length(); int
m = str2.length(); int
k = str3.length(); if
(n + m != k) return
false ; vector<vector< bool > > dp(n+1); for
( int
i = 0; i < n+1; i++) { dp[i].resize(m+1); } dp[0][0] = true ; for
( int
i = 0; i <= n; i++) { for
( int
j = 0; j <= m; j++) { if
(i == 0 && j == 0) { dp[0][0] = true ; } else
if
(i == 0) { dp[0][j] = dp[0][j-1] && str2[j-1] == str3[j-1]; } else
if
(j == 0) { dp[i][0] = dp[i-1][0] && str1[i-1] == str3[i-1]; } else
{ dp[i][j] = (dp[i-1][j] && str1[i-1] == str3[i+j-1]) || (dp[i][j-1] && str2[j-1] == str3[i+j-1]); } } } return
dp[n][m]; } |
原文:http://www.cnblogs.com/lautsie/p/3526347.html