题目链接:http://poj.org/problem?id=2250

思路分析:最长公共子序列问题的变形,只是把字符变成了字符串,按照最长公共子序列的思路即可以求解。

代码如下:

#include <stdio.h>
#include <string.h> #define IsEqual(a, b) strcmp((a), (b)) == 0
enum { Left, Up, UpAndLeft };
int XLen, YLen;
const int MAX_N = + ;
char X[MAX_N][], Y[MAX_N][];
int dp[MAX_N][MAX_N], r[MAX_N][MAX_N]; void PrintWords(int i, int j)
{
if (i == || j == )
return; if (r[i][j] == UpAndLeft)
{
PrintWords(i - , j - ); if (i == XLen && j == YLen)
printf("%s",X[i]);
else
printf("%s ", X[i]);
}
else
if (r[i][j] == Up)
PrintWords(i - , j);
else
PrintWords(i, j - );
} void Lcs( int XLen, int YLen )
{
for (int i = ; i <= XLen; ++i)
dp[i][] = ;
for (int j = ; j <= YLen; ++j)
dp[][j] = ; for (int i = ; i <= XLen; ++i)
for (int j = ; j <= YLen; ++j)
{
if (IsEqual(X[i], Y[j]))
{
dp[i][j] = dp[i - ][j - ] + ;
r[i][j] = UpAndLeft;
}
else
if (dp[i - ][j] >= dp[i][j - ])
{
dp[i][j] = dp[i - ][j];
r[i][j] = Up;
}
else
{
dp[i][j] = dp[i][j - ];
r[i][j] = Left;
}
}
} int main()
{
XLen = YLen = ; while (scanf("%s", X[XLen]) != EOF)
{
memset(dp, , sizeof(dp));
memset(r, -, sizeof(r)); while ()
{
scanf("%s", X[++XLen]);
if (strcmp("#", X[XLen]) == )
{
XLen--;
break;
}
} while ()
{
scanf("%s", Y[YLen++]);
if (strcmp("#", Y[YLen - ]) == )
{
YLen -= ;
break;
}
} Lcs(XLen, YLen);
PrintWords(XLen, YLen);
printf("\n");
XLen = YLen = ;
} return ;
}

最新文章

  1. Itext Demo
  2. 教你用netstat-实践案例
  3. mybatis 如何查找表里的某一个字段,然后返回它们的结果集list ?
  4. 清理SQL Server服务器名称列表
  5. ECshop网店系统百万级商品量性能优化-简单的一些Cache内存配置
  6. poj 1769 Minimizing maximizer 线段树维护dp
  7. wpf XAML xaml 进行 数据绑定,Resource DataContext ElementName
  8. AngularJS学习篇(六)
  9. java---- XMLEncoder 和 XMLDecoder 和 xSteam工具使用
  10. 【洛谷3865】 【模板】ST表(猫树)
  11. 史上最完整的MySQL注入
  12. HTML中的元素定位
  13. pyg 图片服务器中使用的nginx 编译位置
  14. c#中ofType的用法
  15. json模块和pickle模块(二十二)
  16. django --- DetailView源码分析
  17. C#支付宝多次回调问题
  18. Java学习之路(十二):IO流
  19. 数论入门2——gcd,lcm,exGCD,欧拉定理,乘法逆元,(ex)CRT,(ex)BSGS,(ex)Lucas,原根,Miller-Rabin,Pollard-Rho
  20. AngularJS Intellisense in Visual Studio 2012

热门文章

  1. XML 文档解析操作
  2. KMP算法的一次理解
  3. HDU2005-第几天
  4. IList, ICollection ,IEnumerable AND IEnumerator in C#
  5. Java多线程编程中Future模式的详解
  6. 盘点:移动服务 #AzureChat
  7. 代码收藏 JS实现页内查找定位功能
  8. golang实现udp接入服务器
  9. codeforces#FF DIV2C题DZY Loves Sequences(DP)
  10. OpenStack_Swift源代码分析——Object-auditor源代码分析(1)