【leetcode】 Scramble String (hard)★
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.
判断两个字符串s1和s2是否可以通过旋转实现。
思路:开始傻眼了,反应不过来。第二天恍然大悟,用递归,分左右部分,变成子问题。但是最开始我以为划分树只有一种方式,后来发现树可以随意的划分,左子树可以有任意个字符,右子树也是。所以需要循环遍历所有条件。再然后是可能s1的左半部分在s2中位于左半部分 或者是右半部分,所以对这两种情况也要都考虑到。
循环的时候用了截枝,如果两边的字符有不一致的直接跳过该次循环。
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
using namespace std; class Solution {
public:
bool isScramble(string s1, string s2) {
if(s1.length() != s2.length())
return false;
if(s1 == s2)
return true; if(!isEqual(s1 , s2))
{
return false;
} for(int i = ; i < s1.length(); i++)
{
int leftlength = i;
int rightlength = s1.length() - leftlength;
string s1left1 = s1.substr(, leftlength);
string s2left1 = s2.substr(, leftlength);
string s1right1 = s1.substr(leftlength, rightlength);
string s2right1 = s2.substr(leftlength, rightlength); string s1left2 = s1.substr(rightlength, leftlength);
string s1right2 = s1.substr(, rightlength); if(!isEqual(s1left1,s2left1) && !isEqual(s1left2,s2left1))
{
continue;
}
if((isScramble(s1left1, s2left1) && isScramble(s1right1, s2right1)) || (isScramble(s1left2, s2left1) && isScramble(s1right2, s2right1)))
{
return true;
}
}
return false;
} //判断两个字符串的字母组成是否一致
bool isEqual(string s1, string s2)
{
if(s1.length() != s2.length())
return false;
for(int i = ; i < s1.length(); i++)
{
int locate = s2.find(s1[i]);
if(locate == string::npos)
{
return false;
}
else
{
s2.erase(s2.begin() + locate);
}
}
return true;
}
}; int main()
{
Solution s;
string s1 = "oatzzffqpnwcxhejzjsnpmkmzngneo";
string s2 = "acegneonzmkmpnsjzjhxwnpqffzzto";
//string s1 = "gneo";
//string s2 = "gneo";
bool ans = s.isScramble(s1, s2); return ;
}
有大神给出了正宗动态规划的解法,非常精简,要好好学习。 不过这个方法算了所有的情况所以时间比我的要长,差不多150ms, 我的是50ms左右。
dp[i][j][l] means whether s2.substr(j,l) is a scrambled string of s1.substr(i,l) or not.
class Solution {
public:
    bool isScramble(string s1, string s2) {
        int len=s1.size();
        bool dp[][][]={false};
        for (int i=len-;i>=;i--)
            for (int j=len-;j>=;j--) {
                dp[i][j][]=(s1[i]==s2[j]);
                for (int l=;i+l<=len && j+l<=len;l++) {
                    for (int n=;n<l;n++) { //所有划分左右区间的情况
                        dp[i][j][l]|=dp[i][j][n]&&dp[i+n][j+n][l-n]; //s1的左边对s2的左边
                        dp[i][j][l]|=dp[i][j+l-n][n]&&dp[i+n][j][l-n];//s1的左边对s2的右边
                    }
                }
            }
        return dp[][][len];
    }
}; 
【leetcode】 Scramble String (hard)★的更多相关文章
- 【leetcode】Scramble String
		Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ... 
- 【LeetCode】字符串 string(共112题)
		[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ... 
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ... 
- 【LeetCode】984. String Without AAA or BBB 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ... 
- 【leetcode】 Interleaving String  (hard)
		Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ... 
- 【leetcode】Interleaving String
		Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ... 
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
		题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ... 
- 【leetcode】8. String to Integer (atoi)
		题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ... 
- 【leetcode】443. String Compression
		problem 443. String Compression Input ["a","a","b","b"," ... 
随机推荐
- 使用SandCastle创建.Net帮助文档
			使用SandCastle创建.Net帮助文档 引用自:http://www.cnblogs.com/DotNetNuke/archive/2009/04/23/1441899.html Sandcas ... 
- PHP预编译处理技术简介
			1.提高数据库的效率:减少编译次数,减少连接次数.当出现当量操作sql语句,比如大量将数据插入数据库中,原来的那种单个执行sql语句或者批量执行sql语句的做法,显然是不可行的,因为无论是单个执行还是 ... 
- 牡丹江.2014k(构造)
			K - Known Notation Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Su ... 
- 给Windows + Apache 2.2 + PHP 5.3 安装PHP性能测试工具 xhprof_0.10.3_php53_vc9.dll
			1.下载XHProf 到这里 http://dev.freshsite.pl/php-extensions/xhprof.html 下载Windows版本的XHProf,我这里选择下载 XHProf ... 
- 如何把你的图标转换成web字体
			在这篇教程中,我们将使用一个免费的Web应用程序IcoMoon将矢量图转换成Web字体,然后将生成的字体通过css应用到Web页面中. 通常我们在网站中必不可少的会使用到一些小图标.在正常尺寸下,布局 ... 
- centos systemctl指令
			# systemctl #输出已激活单元 # systemctl list-units #输出已激活单元 # systemctl --failed #输出运行失败的单元 # systemctl lis ... 
- ASP.NET、C#调用外部可执行exe文件--多种方案
			一. try { //方法一 //调用自己的exe传递参数 //Process proc = new Process(); //proc.StartInfo.FileName = @"D:\ ... 
- 【转】将datatable数据转化成list
			#region 将datatable数据转化成list public static List<T> ToList<T>(this DataTable dt) where T ... 
- osx xcode 创建python项目
			http://stackoverflow.com/questions/5276967/python-in-xcode-7 
- 基于php的snmp管理端开发
			一.系统环境: 操作系统:CentOS 5.4 内核:Linux_2.6 编译环境:gcc 4.1.2 代码版本:php-5.2.8.tar ... 
