C++ 检查一个字符串是否与另外两个字符串交错
我正在调试以下问题。发布详细的问题陈述和编码。我的问题是最后一个else if(C++ 检查一个字符串是否与另外两个字符串交错,c++,algorithm,C++,Algorithm,我正在调试以下问题。发布详细的问题陈述和编码。我的问题是最后一个else if(else if(A[i-1]==C[i+j-1]&&B[j-1]==C[i+j-1]))是否必要?我认为这是没有必要的,因为如果(A[I-1]==C[I+j-1]&&B[j-1]!=C[I+j-1]),或者如果(A[I-1]!=C[I+j-1]&&B[j-1]==C[I+j-1]),即前两个if-else检查条件,它总是被覆盖。谢谢 给定s1、s2、s3,找出s3是否由s1和s2交错形成 比如说,, 鉴于: s1=“
else if(A[i-1]==C[i+j-1]&&B[j-1]==C[i+j-1])
)是否必要?我认为这是没有必要的,因为如果(A[I-1]==C[I+j-1]&&B[j-1]!=C[I+j-1]),或者如果(A[I-1]!=C[I+j-1]&&B[j-1]==C[I+j-1]),即前两个if-else检查条件,它总是被覆盖。谢谢
给定s1、s2、s3,找出s3是否由s1和s2交错形成
比如说,,
鉴于:
s1=“aabcc”,
s2=“dbbca”
当s3=“aadbbcac”时,返回true。
当s3=“aadbbbaccc”时,返回false
// The main function that returns true if C is
// an interleaving of A and B, otherwise false.
bool isInterleaved(char* A, char* B, char* C)
{
// Find lengths of the two strings
int M = strlen(A), N = strlen(B);
// Let us create a 2D table to store solutions of
// subproblems. C[i][j] will be true if C[0..i+j-1]
// is an interleaving of A[0..i-1] and B[0..j-1].
bool IL[M+1][N+1];
memset(IL, 0, sizeof(IL)); // Initialize all values as false.
// C can be an interleaving of A and B only of sum
// of lengths of A & B is equal to length of C.
if ((M+N) != strlen(C))
return false;
// Process all characters of A and B
for (int i=0; i<=M; ++i)
{
for (int j=0; j<=N; ++j)
{
// two empty strings have an empty string
// as interleaving
if (i==0 && j==0)
IL[i][j] = true;
// A is empty
else if (i==0 && B[j-1]==C[j-1])
IL[i][j] = IL[i][j-1];
// B is empty
else if (j==0 && A[i-1]==C[i-1])
IL[i][j] = IL[i-1][j];
// Current character of C matches with current character of A,
// but doesn't match with current character of B
else if(A[i-1]==C[i+j-1] && B[j-1]!=C[i+j-1])
IL[i][j] = IL[i-1][j];
// Current character of C matches with current character of B,
// but doesn't match with current character of A
else if (A[i-1]!=C[i+j-1] && B[j-1]==C[i+j-1])
IL[i][j] = IL[i][j-1];
// Current character of C matches with that of both A and B
else if (A[i-1]==C[i+j-1] && B[j-1]==C[i+j-1])
IL[i][j]=(IL[i-1][j] || IL[i][j-1]) ;
}
}
return IL[M][N];
}
//如果C为
//A和B的交错,否则为假。
布尔是交错的(char*A,char*B,char*C)
{
//求两个字符串的长度
int M=strlen(A),N=strlen(B);
//让我们创建一个二维表来存储
//如果C[0..i+j-1],子问题C[i][j]将为真
//是A[0..i-1]和B[0..j-1]的交错。
bool-IL[M+1][N+1];
memset(IL,0,sizeof(IL));//将所有值初始化为false。
//C只能是A和B的和的交错
//A和B的长度等于C的长度。
如果((M+N)!=strlen(C))
返回false;
//处理A和B的所有字符
对于(int i=0;i您确实需要最后一个else if
来捕获C中的下一个字符与A和B中的下一个字符匹配的情况。例如,使用A=“aaaa”
、B=“aaaa”
和C=“aaaaaaaa”
块运行程序,并查看是否输入最后一个else if
块
此外,您还需要一个最终的else
块来处理前面任何条件都不匹配的情况。在这种情况下,您需要将IL[i][j]
设置为false
。否则,函数将错误地返回true
编辑:即使代码使用memset
将IL
的所有元素初始化为0
,它也可能不起作用,因为可变长度数组(VLAs)。事实上,这是在I时发生的。它使用g++-4.9.2,带有标志,导致它报告sizeof(IL)
成为1
,即使g++
应该支持VLA。可能这是一个编译器错误,也可能它不支持多维VLA。在任何情况下,完全不使用它们可能更安全。我想您确实需要最后一种情况。例如,当a=“aaaa”
,B=“aaaa”时会发生什么
和C=“aaaaaaaa”
。此外,您的算法中还有其他问题。例如,我看不到您在哪里将IL[][/code>的任何元素设置为false
。您的函数可能总是返回true
。您可能需要在IL[I][j]的位置设置一个最终的else
块
应设置为false
。在最后一个else if
块中放置断点或打印,然后查看在我上面提到的示例中程序是否到达那里。正如代码中的注释所述,前面两个else if
语句将无法捕获C中的字符与a中的字符匹配的情况nd B.我认为这样更干净。但是,我确实建议用std::string
替换char*
的用法。如果你想继续使用char*
,至少声明参数const char*
,因为你没有修改它们。这样你可以用字符串和const
调用函数>字符串。例如,isInterleaved(“aaa”,b,c)
。完成。但不要犹豫。这是可以的,也是值得鼓励的。:D