解题思路:有大佬帮忙看看问题在哪里吗,我的思路就是求公共子序列啊!!!
注意事项:
参考代码:
#include<stdio.h>
#include<string.h>
int m = 0, k1 = 0, k2 = 0;
char a_1[1005] = { 0 };
char b_1[1005] = { 0 };
char a[2000][1005] = { 0 };
char b[2000][1005] = { 0 };
int c[1005][1005] = { 0 };
int main()
{
gets(a_1);
gets(b_1);
int n_a = 0, n_b = 0;
n_a = strlen(a_1);
n_b = strlen(b_1);
for (int i = 0; i < n_a; i++)
{
if (a_1[i] >= 95 && a_1[i] <= 122 || i == 0)
a[k1][m++] = a_1[i];
else
{
k1++;
m = 0;
a[k1][m++] = a_1[i];
}
}
m = 0;//下标置零;
for (int i = 0; i < n_b; i++)
{
if (b_1[i] >= 95 && b_1[i] <= 122 || i == 0)
b[k2][m++] = b_1[i];
else
{
k2++;
m = 0;
b[k2][m++] = b_1[i];
}
}
//for (int i = 0; i <= k2; i++)
//{
// printf("%s\n", b[i]);
//}//测试用;
for (int i = 0; i < n_a+3;i++)
for (int j = 0; j < n_b+3; j++)//放大数组;
{
c[i][j] = 0;
}
int q = 2, p = 2;
for (int i = 0; i <= k1;i++)
for (int j = 0; j <= k2; j++)
{
if (strcmp(a[i], b[j]) == 0)
{
c[q + i][p + j] = c[q + i - 1][p + j - 1] + 1;
}
else
c[q + i][p + j] = c[q + i][p + j - 1]>c[q + i - 1][p + j] ? c[q + i][p + j - 1] : c[q + i - 1][p + j];
}
//printf("%d", c[q + k1][p + k2]);
for (int i = 0; i < n_a; i++)
{
for (int j = 0; j < n_b; j++)
{
printf("%-3d", c[i][j]);
}
printf("\n");
}//测试用;
}
0.0分
0 人评分