大致题意:就是求两篇文章的最长公共子序列并打印
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 105, L = 35;
char a[N][L], b[N][L];
int pre[N][N], d[N][N], n, m, la, lb, flag;
void lcs()
{
memset (d, 0, sizeof (d));
memset (pre, 0, sizeof (pre));
for (int i = 1; i < la; ++i)
{
for (int j = 1; j < lb; ++j)
{
if (!strcmp (a[i], b[j]))
{
d[i][j] = d[i - 1][j - 1] + 1;
pre[i][j] = 1;
}
else if (d[i - 1][j] > d[i][j - 1])
{
d[i][j] = d[i - 1][j];
pre[i][j] = 2;
}
else
{
d[i][j] = d[i][j - 1];
pre[i][j] = 3;
}
}
}
}
void print (int i, int j)
{
if (pre[i][j] == 1)
{
print (i - 1, j - 1);
if (flag) flag = 0;
else printf (" ");
printf ("%s", a[i]);
}
else if (pre[i][j] == 2)
print (i - 1, j);
else if (pre[i][j] == 3)
print (i, j - 1);
}
int main()
{
while (scanf ("%s", a[1]) != EOF)
{
la = 1;
lb = 0;
flag = 1;
while (scanf ("%s", a[++la]) && a[la][0] != '#');
while (scanf ("%s", b[++lb]) && b[lb][0] != '#');
lcs();
print (la - 1, lb - 1);
printf ("\n");
}
return 0;
}