LeetCode_Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.
Below is one possible representation of s1 = "great":
great
/ \
gr eat
/ \ / \
g r e at
/ \
a t
To scramble the string, we may choose any non-leaf node and swap its two children.
For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".
rgeat
/ \
rg eat
/ \ / \
r g e at
/ \
a t
We say that "rgeat" is a scrambled string of "great".
Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".
rgtae
/ \
rg tae
/ \ / \
r g ta e
/ \
t a
We say that "rgtae" is a scrambled string of "great".
Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
三维动态规划: f(i, j, n) = || ((f(i, j, m) && f(i + m, j + m, n - m)) || f(i, j + n - m, m) && f(i + m, j, n - m)) for 1 <= m < n where f(i, j, n) is true iff substring starts at s1[i] and substring starts at s2[j] both with length n are scrambled
class Solution {
public:
bool isScramble(string s1, string s2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n = s1.size();
if (s2.size() != n) return false;
if(s1 == s2 ) return true;
bool f[n][n][n+];
for(int i= ; i< n; i++)
for(int j = ; j< n; j++)
{
f[i][j][] = true;//len 0 其实没用,只是为了k 的编程容易而已
f[i][j][] = s1[i] == s2[j] ;
}
for(int len = ; len <= n; len ++)
for( int i = ; i + len - < n;i++)
for(int j = ; j + len - <n;j++)
{
f[i][j][len] = false;
for(int k = ; k< len ; k++){
if ( (f[i][j][k] && f[i+k][j+k][len-k]) ||
(f[i][j+len-k][k] && f[i+k][j][len-k]) )
{
f[i][j][len] = true;
break;
}
}
}
return f[][][n];
}
};
LeetCode_Scramble String的更多相关文章
- 透过WinDBG的视角看String
摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...
- JavaScript String对象
本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密
string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...
- js报错: Uncaught RangeError: Invalid string length
在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...
- JavaScript中String对象的方法介绍
1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...
- 在多线程编程中lock(string){...}隐藏的机关
常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...
随机推荐
- rsyslog 一重启就会开始同步之前所有通配的日志文件
<pre name="code" class="html">[root@dr-mysql01 zjzc_log]# grep '24/Sep/201 ...
- 开启Apache mod_rewrite模块(解决404 Not Found)
网站搭建完成了,进入登录界面就是访问不了. 原因大概是没有开启Apache mod_rewrite模块,或者没有配置完全. 步骤1: 启用mod_rewrite模块 在conf目录的httpd.con ...
- Numerical Methods with MATLAB(1)
目前正在阅读MATLAB相关的书籍:Numerical Methods with MATLAB,现在感觉这本书写的还行, 细致基础,而且写的比较清楚,同时把malab和数值算法结合在一起. 目前刚看完 ...
- 黑马程序员_JavaIO流(三)
字节流File读写操作 字符流: FileReader FileWriter BufferedReader BufferedWrtier 字节流: FileInputStream FileOutput ...
- sicily 4379 bicoloring
题意:输入一个简单(无多重边和自环)的连通无向图,判断该图是否能用黑白两种颜色对顶点染色,使得每条边的两个端点为不同颜色. 解法:由于无自连通节点存在,所以只需进行一次宽搜,遍历所有的点和所有的边,判 ...
- UVa400.Unix ls
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- hadoop-2.6.0为分布式安装
hadoop-2.6.0为分布式安装 伪分布模式集群规划(单节点)------------------------------------------------------------------- ...
- 形形色色的软件生命周期模型(4)——MSF、实用型
摘要: 读大学时,我们曾经学习过不少软件生命周期模型,当时还不是很懂软件开发,你可能会觉得这些东西很新奇.在实际工作中,你会发现这些模型其实很难应用,与此同时你会接触到RUP.MSF等权威软件公司的生 ...
- java随机数生成器
一.java.lang.Math.Random 调用这个Math.Random()函数能够返回带正号的double值,取值范围是[0.0,1.0)的左闭右开区间,并在该范围内(近似)均匀分布. 二.j ...
- 在Build时使用NuGet自动下载缺失的包
现在使用.net编程,不可避免的总会使用到NuGet去下载引用第三方的dll,而且vs2012也直接默认就会安装上NuGet了.NuGet的确也给我们带来了一些方便,但是也带来了一些问题,比如:一般程 ...