1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| unordered_map<string, int> dp;
int dnc(string A, string B) { string k = A + "#" + B; if(A.length() != B.length()) return false; if(A == B) return true; if(A.length() == 1) return false; if(dp.count(k)) return dp[k]; dp[k] = 0; for(int cut = 1; cut < A.length() and !dp[k]; cut++) { string caf = A.substr(0,cut), cab = A.substr(cut); string cbf = B.substr(0,cut), cbb = B.substr(cut); string rcbf = B.substr(0, B.length() - cut), rcbb = B.substr(B.length() - cut); dp[k] |= dnc(caf, cbf) and dnc(cab, cbb); dp[k] |= dnc(caf,rcbb) and dnc(cab,rcbf); } return dp[k]; }
int isScramble(const string A, const string B) { return dnc(A,B); }
|