[LeetCode] Scramble String 字符串 dp
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.
class Solution {
public:
bool isScramble(string s1, string s2) {
int len1 = s1.size(),len2 = s2.size();
if(help_f(s1,s2)){
if(s1==s2) return true;
for( int i =;i<len1;i++){
if(s1.substr(,i)+s1.substr(i)==s2) return true;
}
for( int i=;i<len1;i++){
if(isScramble(s1.substr(,i),s2.substr(,i))&&isScramble(s1.substr(i),s2.substr(i)))
return true;
// cout<<s1.substr(0,i)<<" "<<s2.substr(len2-i)<<" "<<s1.substr(len1-i)<<" "<<s2.substr(0,i)<<endl;
if(isScramble(s1.substr(,i),s2.substr(len2-i))&&isScramble(s1.substr(i),s2.substr(,len2-i)))
return true;
}
}
return false;
}
bool help_f(string &s1,string &s2)
{
if(s1.size()!=s2.size()) return false;
int c[]={};
for(int i=;i<s1.size();i++) c[s1[i]-'a'] ++;
for(int i=;i<s2.size();i++){
c[s2[i]-'a']--;
if(c[s2[i]-'a']<) return false;
}
return true;
}
};
class Solution
{
public:
bool isScramble(string s1, string s2)
{
int len1=s1.size(),len2=s2.size();
if(len1!=len2) return false;
bool table[][][]={false};
for(int i=len1-;i>=;i--){
for(int j=len1-;j>=;j--){
table[i][j][]=(s1[i]==s2[j]);
for(int tmpLen=;i+tmpLen<=len1&&j+tmpLen<=len1;tmpLen++){
for(int idx=;idx<tmpLen;idx++){
table[i][j][tmpLen]|=table[i][j][idx]&&table[i+idx][j+idx][tmpLen-idx];
table[i][j][tmpLen]|=table[i][j+tmpLen-idx][idx]&&table[i+idx][j][tmpLen-idx];
}
}
}
}
return table[][][len1];
}
};
[LeetCode] Scramble String 字符串 dp的更多相关文章
- [leetcode]87. Scramble String字符串树形颠倒匹配
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- [LeetCode] Scramble String -- 三维动态规划的范例
(Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...
- [LeetCode] Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [Leetcode] scramble string 乱串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [leetcode]Scramble String @ Python
原题地址:https://oj.leetcode.com/problems/scramble-string/ 题意: Given a string s1, we may represent it as ...
- [Leetcode] Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] Scramble String(树的问题最易用递归)
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [每日一题2020.06.09] leetcode #97 交错字符串 dp
题目链接 利用动态规划的思想, 对于每种状态(i, j)来说都有(i-1, j) 和 (i,j-1) 需要注意的问题 : 初始化的问题,先把i=0和j=0的状态都初始化后才可以进行dp否则发生数组越界 ...
随机推荐
- WebUploader压缩图片上传
WebUploader,由Baidu FEX 团队开发,以H5为主,FLASH为辅,兼容 IE6+,iOS 6+, android 4+,采用大文件分片并发上传,极大的提高了文件上传效率,看了官方文档 ...
- 【php】如何配置自主域名腾讯企业邮箱
腾讯企业邮配置 protocal ssl smtp port 465 host smtp.exmail.qq.com user email account passwd email passwd
- POJ 2586 贪心+枚举
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15626 Accepted: 78 ...
- uva1422 二分法+优先队列贪心
题意:有n个任务,每个任务必须在在时刻[r, d]之内执行w的工作量(三个变量都是整数).处理器执行的速度可以变化,当速度为s时,一个工作量为w的任务需要 执行的时间为w/s个单位时间.另外不一定要连 ...
- cut(树形DP)
Description F大爷热爱切树.今天他找到一棵黑白树,不到两秒钟,F大爷就把这棵树切掉了.已知原先树上共n个点,每个点都是黑点或者白点,F大爷切去若干条边后,分成的若干个连通子树中每块恰有一个 ...
- python基础——18(面向对象2+异常处理)
一.组合 自定义类的对象作为另一个类的属性. class Teacher: def __init__(self,name,age): self.name = name self.age = age t ...
- 3282: Tree(LCT)
3282: Tree Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 2249 Solved: 1042[Submit][Status][Discus ...
- C语言编程题001
有一颗树,一年两个生长周期,春天它长高一倍,夏天长高1m,问N个周期后树有多高?假设从春天开始树高为1m,第0个周期树高为1m. 要求:1.可以同时输入多个生长周期 如:3//表示下面有几个生长周期 ...
- git:多个sshkey配置
克隆项目: 使用git clone +项目.git地址 例如: 创建SSH Key: ssh-keygen -t rsa -C +邮箱地址 sshkey自定义保存:创建后在第二步(enter file ...
- CSU-2049 象棋
CSU-2049 象棋 Description 車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子.一天,小度在棋盘上摆起了许多車--他想知道,在一共N×M个点的矩形棋盘中摆最多 ...