本博客rss订阅地址: http://feed.cnblogs.com/blog/u/147990/rss

LeetCode:Interleaving String

题目链接

Given s1s2s3, find whether s3 is formed by the interleaving of s1 and s2.

For example,
Given:
s1 = "aabcc",
s2 = "dbbca",

When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.

开始理解错了题意,以为s3可以包含多个s1或者s2。如果匹配的话,s3只包含一个s1和s2

递归解法:每次比较s1和s3的最后一个字符或者s2和s3的最后一个字符,如果相等去掉两者的最后一个字符进入子问题,只要一个子问题返回true,父问题就返回true

 isInterleave(s1[0...n1], s2[0...n2], s3[0...n3]) = (s1[n1] == s3[n3] && isInterleave(s1[0...n1-1], s2[0...n2], s3[0...n3-1])) || (s2[n2] == s3[n3] && isInterleave(s1[0...n1], s2[0...n2-1], s3[0...n3-1]))

 1 class Solution {
 2 public:
 3     bool isInterleave(string s1, string s2, string s3) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.    
 6         return isInterleaveRecur(s1, s1.length()-1, s2, s2.length()-1, s3, s3.length()-1);
 7     }
 8     bool isInterleaveRecur(string &s1, int end1, string &s2, int end2, string &s3, int end3)
 9     {
10         if(end1 == -1 && end2 == -1 )
11         {
12             if(end3 == -1)
13                 return true;
14             else return false;
15         }
16         if(end1 >=0 && s1[end1] == s3[end3] && isInterleaveRecur(s1, end1-1, s2, end2, s3, end3-1))
17             return true;
18         if(end2 >=0 && s2[end2] == s3[end3] && isInterleaveRecur(s1, end1, s2, end2-1, s3, end3-1))
19             return true;
20         return false;
21     }
22 };

动态规划解法:根据递归的思想,我们一刻如下使用动态规划解此题。设dp[i][j]表示s1[0...i-1]和s2[0...j-1]能否组合成s3[0....i+j-1],动态规划方程如下                                                                               本文地址

  • dp[i][j] = (dp[i][j-1] ==true && s3[i+j-1] == s2[j-1]) || (dp[i-1][j] == true && s3[i-1+j] == s1[i-1])
  • 初始条件:if(s1[0] == s3[0])dp[1][0] = true  ,  if(s2[0] == s3[0])dp[0][1] = true; 其他值均为false
 1 class Solution {
 2 public:
 3     bool isInterleave(string s1, string s2, string s3) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.    
 6         const int len1 = s1.length(), len2 = s2.length(), len3 = s3.length();
 7         if(len1 == 0)return s2 == s3;
 8         else if(len2 == 0)return s1 == s3;
 9         // dp[i][j]表示s1[0...i-1]和s2[0...j-1]能否组合成s3[0....i+j-1]
10         bool dp[len1+1][len2+1];
11         memset(dp, 0, sizeof(dp));
12         if(len3 != 0)
13         {
14             if(s1[0] == s3[0])dp[1][0] = true;
15             if(s2[0] == s3[0])dp[0][1] = true;
16         }
17         else return false;// len3 = 0,但是len1和len2不等于0
18         if(len1 + len2 != len3)return false;//长度关系不满足
19         for(int i = 0; i <= len1; i++)
20             for(int j = 0; j <= len2; j++)
21             {
22                 int tmp = i+j-1;
23                 if(j > 0)
24                 {
25                     if(dp[i][j-1] && s3[tmp] == s2[j-1])
26                         {dp[i][j] = true; continue;}
27                 }
28                 if(i > 0)
29                 {
30                     if(dp[i-1][j] && s3[tmp] == s1[i-1]) 
31                         dp[i][j] = true;
32                 }
33             }
34         if(dp[len1][len2])return true;
35         else return false;
36     }
37 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3448469.html

 

posted @ 2013-11-28 21:14  tenos  阅读(685)  评论(0编辑  收藏  举报

本博客rss订阅地址: http://feed.cnblogs.com/blog/u/147990/rss

公益页面-寻找遗失儿童