Uva10405 Longest Common Subsequence
2014-04-02 18:29
453 查看
链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1346
题意:经典LCS问题,最长公共子序列,状态转移方程if(a[i]=b[j])dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int m = 1000+10;
int dp[m][m];
int main()
{
char a[m];
char b[m];
while(gets(a)&&gets(b))
{
int l=strlen(a);
int h=strlen(b);
memset(dp,0,sizeof(dp));
for(int i=1;i<=l;i++){
for(int j=1;j<=h;j++)
{
if(a[i-1]==b[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
cout << dp[l][h] << endl;
}
}
题意:经典LCS问题,最长公共子序列,状态转移方程if(a[i]=b[j])dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int m = 1000+10;
int dp[m][m];
int main()
{
char a[m];
char b[m];
while(gets(a)&&gets(b))
{
int l=strlen(a);
int h=strlen(b);
memset(dp,0,sizeof(dp));
for(int i=1;i<=l;i++){
for(int j=1;j<=h;j++)
{
if(a[i-1]==b[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
cout << dp[l][h] << endl;
}
}
相关文章推荐
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research