[leetcode刷题系列]Scramble String
来源:互联网 发布:手机图片尺寸修改软件 编辑:程序博客网 时间:2024/06/05 15:24
这题也没说数据规模, 一开始按照100来的。 跑大数据的时候TLE了。 于是怀疑是不是每次初始化花太多时间了
所以就改成了50就过了- -,大概是n^4的算法, 有点暴力了。 不知道有没有其他更好的。当然说是n^4,实际执行效率肯定
远好于n^4的估算。
const int MAXN = 50 + 5;string s1, s2;int dp[MAXN][MAXN][MAXN];int get(int st1, int st2, int len){ int& ret = dp[st1][st2][len]; if(ret != -1) return ret; if(len == 1) return ret = s1[st1] == s2[st2]; for(int i = 1; i < len; ++ i){ // 1 if(get(st1, st2, i) && get(st1 + i, st2 + i, len - i)) return ret = 1; if(get(st1, st2 + len - i, i) && get(st1 + i, st2, len - i)) return ret = 1; } return ret = 0;}class Solution {public: bool isScramble(string s1, string s2) { // Start typing your C/C++ solution below // DO NOT write int main() function if(s1.size() != s2.size()) return false; ::s1 = s1; ::s2 = s2; memset(dp, 0xff, sizeof(dp)); return get(0, 0, s1.size()); }};
- [leetcode刷题系列]Scramble String
- LeetCode刷提笔录Scramble String
- LeetCode : Scramble String
- [LeetCode] Scramble String
- [LeetCode] Scramble String
- [Leetcode] Scramble String
- [leetcode] Scramble String
- LeetCode: Scramble String
- [LeetCode]Scramble String
- [leetcode]Scramble String
- Leetcode:Scramble String
- LeetCode-Scramble String
- LeetCode Scramble String
- [leetcode] Scramble String
- [LeetCode] Scramble String
- LeetCode - Scramble String
- [LeetCode]Scramble String
- [LeetCode] Scramble String
- 功能强大的JavaScript引擎--SpiderMonkey
- 2013.8.13
- Javascript中window对象、document对象的属性和方法(转)
- 用 VBScript 的 ADODB 连接 MySQL 数据库
- 杂志摄影师信手成经典 揭秘XP“蓝天白云”桌面真景
- [leetcode刷题系列]Scramble String
- 结构体作为函数参数
- HDU4675 GCD of Sequence
- win8(摄像头)相机打不开是怎么回事?
- Android真机/模拟器hosts修改
- 黑马程序员_08集合——Map
- 开发者常用的6款源码搜索引擎
- 黑马程序员_静态及单例设计模式
- UDP网络通信的程序设计