//公共字串计算
#include <iostream>
#include <string>
using namespace std;
int getCommonStrLength(char * pFirstStr, char * pSecondStr)
{
int len1 = strlen(pFirstStr);
int len2 = strlen(pSecondStr);
char *s1 = pFirstStr;
char *s2 = pSecondStr;
if (len1 == 0 || len2 == 0)
{
cout << 0 << endl;
return 0;
}
int s[100][100] = {0};
for (int i = 0; i < len1; i++)
{
if((s1[i] == s2[0]))
s[i][0] = 1;
}
for (int i = 0; i < len2; i++)
{
if (s1[0] == s2[i])
s[0][i] = 1;
}
for (int i = 1; i < len1; i++)
{
for (int j = 1; j < len2; j++)
{
if (s1[i] == s2[j])
s[i][j] = s[i-1][j-1] + 1;
else
s[i][j] = 0;
}
}
int max=0;
for (int i = 0; i < len1; i++)
for (int j = 0; j < len2; j++)
{
if (s[i][j] > max)
max = s[i][j];
}
cout << max << endl;
return 0;
}
int main()
{
char s1[1000];
char s2[1000];
cin >> s1;
cin >> s2;
strupr(s1);
strupr(s2);
int i = getCommonStrLength(s1, s2);
return 0;
}
公共字串计算
最新推荐文章于 2022-04-21 20:38:04 发布