DP。注意曾经把赋值写成了==,结果出错半天。
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];
}