首页 > 其他 > 详细

交叉字符串

时间:2017-08-06 23:41:43      阅读:196      评论:0      收藏:0      [点我收藏+]

代码:

class Solution {
public:
/**
* Determine whether s3 is formed by interleaving of s1 and s2.
* @param s1, s2, s3: As description.
* @return: true of false.
*/
bool isInterleave(string s1, string s2, string s3) {
// write your code here
if(s3.length()!=s1.length()+s2.length())
return false;
if(s1.length()==0)
return s2==s3;
if(s2.length()==0)
return s1==s3;
vector<vector<bool> > dp(s1.length()+1,vector<bool>(s2.length()+1,false));
dp[0][0] = true;
for(int i=1;i<=s1.length();i++)
dp[i][0] = dp[i-1][0]&&(s3[i-1]==s1[i-1]);
for(int i=1;i<=s2.length();i++)
dp[0][i] = dp[0][i-1]&&(s3[i-1]==s2[i-1]);
for(int i=1;i<=s1.length();i++)
{
for(int j=1;j<=s2.length();j++)
{
int t = i+j;
if(s1[i-1]==s3[t-1])
dp[i][j] = dp[i][j]||dp[i-1][j];
if(s2[j-1]==s3[t-1])
dp[i][j] = dp[i][j]||dp[i][j-1];
}
}
return dp[s1.length()][s2.length()];
}
};

截图:

技术分享

交叉字符串

原文:http://www.cnblogs.com/w1500802028/p/7296462.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!