题目:https://leetcode-cn.com/problems/scramble-string/
参考:https://leetcode-cn.com/problems/scramble-string/solution/c-dong-tai-gui-hua-by-da-li-wang-36/
class Solution {
public:
bool isScramble(string s1, string s2) {
/*
*给出两个长度相等的字符串 s1 和 s2,
*判断 s2 是否是 s1 的扰乱字符串
*区间dp
*定义dp[len][i][j]表示s1.substr(i,len)
*和s2.substr(j,len)是否能通过扰乱匹配
*最后答案即为dp[len][1][1]
*/
int n = s1.length();
vector<vector<vector<bool> > > dp;
dp.resize(n+1);
for(int i = 0;i <= n;i++) {
dp[i].resize(n+1);
for(int j = 0;j <= n;j++) {
dp[i][j].resize(n+1);
}
}
s1 = "#"+s1;
s2 = "#"+s2;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++) {
if(s1[i] == s2[j]) dp[1][i][j] = 1;
}
for(int len = 2;len <= n;len++) {
for(int i = 1;i+len-1 <= n;i++) {
for(int j = 1;j+len-1 <=n;j++) {
for(int k = 1;k < len;k++) {
if(dp[k][i][j] && dp[len-k][i+k][j+k])
dp[len][i][j] = true;
if(dp[k][i][j+len-k] && dp[len-k][i+k][j])
dp[len][i][j] = true;
}
}
}
}
return dp[n][1][1];
}
};