每日一题3.16.2
每日一题3.16.2
解题思路:
现将两个字符串中的大小写字母全部改为小写字母,再依次遍历公共字符串,用临时变量存储,比较出最长的公共字符串
代码实现:
#include <iostream>
#include <string.h>
using namespace std;
#define max(a,b) a > b? a : b
int getCommonStrLength(char *pFirstStr, char *pSecondStr)
{
int count = 0;
int tmp = 0;
int i = 0, j = 0;
int a;
if (pFirstStr == NULL || pSecondStr == NULL)
return 0;
for (i = 0; i < strlen(pFirstStr); i++)
{
for (j = 0; j < strlen(pSecondStr); j++)
{
int a = i, b = j;
while ((pFirstStr[a] == pSecondStr[b]) && a<strlen(pFirstStr) && b<strlen(pSecondStr))
{
a++;
b++;
tmp++;
}
count = max(tmp, count);
tmp = 0;
}
}
cout << count;
return count;
}
int main()
{
char str1[256];
char str2[256];
cin >> str1 >> str2;
for (unsigned int i = 0; i<strlen(str1); i++)
{
str1[i] = tolower(str1[i]);
}
for (unsigned int i = 0; i<strlen(str2); i++)
{
str2[i] = tolower(str2[i]);
}
getCommonStrLength(str1, str2);
system("pause");
return 0;
}
参考答案: