- editorial:
典型的区间dp
定义\(dp[i][j][k]\)表示s1[i: i+k-1]是否是可以通过规则转化为s2[j: j+k-1]。
则动态规划的递推式为:\(dp[i][j][k]|=(dp[i][j][l] \land dp[i+l][j+l][k-l]) \lor (dp[i][j+k-l][l] \land dp[i+l][j][k-l]),k\in [1,k-1]\)
- code:
class Solution {
public:
bool isScramble(string s1, string s2) {
int len1 = s1.length(), len2 = s2.length();
if(len1 != len2) return false;
vector<vector<vector<bool>>> dp(len1, vector<vector<bool>>(len1, vector<bool>(len1+1, false)));
for(int i = 0;i < len1;i++)
for(int j = 0;j < len1;j++)
if(s1[i] == s2[j])
dp[i][j][1] = true;
for(int k = 2;k <= len1;k++)
for(int i = 0;i + k - 1 < len1;i++)
for(int j = 0;j + k - 1 < len1;j++)
for(int l = 1;l < k;l++)
{
dp[i][j][k] = dp[i][j][k] | (dp[i][j][l] & dp[i+l][j+l][k-l]);
dp[i][j][k] = dp[i][j][k] | (dp[i][j+k-l][l] & dp[i+l][j][k-l]);
}
return dp[0][0][len1];
}
};