Poj 1458 Common Subsequence
来源:互联网 发布:手机单机游戏推荐知乎 编辑:程序博客网 时间:2024/06/03 01:36
题目大意:最长公共子序列
思路:DP+滚动数组。
LCS(i,j)定义为 str1 的前i个序列 与 str2 的前j个序列 的最长公共子序列长度。
LCS(i,j)=LCS(i-1,j-1)+1,(str1 [i] == str2 [j])
LCS(i,j)=max( LCS(i-1,j) ,LCS(i,j-1) ),(str1 [i] != str2 [j])
#include <stdio.h>#include <memory.h>#include <string.h>char str1[10010];char str2[10010];int n,m;int dp[2][10010];int LCS() {int i,j,k,ctr;for (i=1;i<=m;i++) {for (j=1;j<=n;j++) {if (str1[i]==str2[j])dp[i%2][j]=dp[(i-1)%2][j-1]+1;else {if (dp[(i-1)%2][j]>dp[i%2][j-1])dp[i%2][j]=dp[(i-1)%2][j];else dp[i%2][j]=dp[i%2][j-1];}}}ctr=dp[m%2][n];return ctr;}int main(){int i;char s1[10010],s2[10010];while(scanf("%s",&s1)!=EOF) {scanf("%s",&s2);memset(dp,0,sizeof(dp));m=strlen(s1);n=strlen(s2);for (i=0;i<m;i++) {str1[i+1]=s1[i];}for (i=0;i<n;i++) {str2[i+1]=s2[i];}printf("%d\n",LCS());}return 0;}
- Common Subsequence--poj--1458
- poj 1458 Common Subsequence
- poj 1458 Common Subsequence
- Poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ-1458-Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- POJ 1458 Common Subsequence
- POJ 1458 Common Subsequence
- POJ 1458 Common Subsequence
- poj 1458 Common Subsequence
- SLES10中vi的backspace键不好用的解决办法
- 如何联机调试和发布iphone程序
- Creating back to top asp.net hyperlink
- 第五章 数组总结
- memcached最大能存储多大的单个item?
- Poj 1458 Common Subsequence
- Linux基础编程-文件操作open,lseek,read,write,close
- 男士正装(1)
- android闹钟开发(四)--音乐播放与音量调节
- 数组传参 和 sizeof
- MySQL语句技巧大放送
- “#ifdef __cplusplus extern "C" { #endif”的定义
- 一个dshow filter的code心得。很简单,但写的还不错
- Drawimg 1.图片按比例缩放 2.加水印并设置水印透明度