LCS 及LCS+滚动数组 模板
来源:互联网 发布:ubc专业 知乎 编辑:程序博客网 时间:2024/05/19 14:38
LCS 模板:
#include<cstdio> #include<cstring>#include<algorithm>using namespace std; char a[1010],b[1010]; int dp[1010][1010]; int main() { int lena,lenb,i,j; while(scanf("%s%s",&a,&b)!=EOF) { lena=strlen(a); lenb=strlen(b); memset(dp,0,sizeof(dp)); for(i=1;i<=lena;++i) { for(j=1;j<=lenb;++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]); } } printf("%d\n",dp[lena][lenb]); } return 0; }
LCS滚动数组模板(当数值太大的时候使用)
//求原串与逆序串的最大公共子序列 #include<cstdio> #include<cstring> #include<algorithm>using namespace std; char str[5050],s[5050]; int dp[2][5050]; int main() { int n,i,j; while(scanf("%d",&n)!=EOF) { memset(dp,0,sizeof(dp)); scanf("%s",str); for(i=0;i<n;++i) s[i]=str[n-1-i]; for(i=1;i<=n;++i) { for(j=1;j<=n;++j) { if(str[i-1]==s[j-1]) dp[i%2][j]=dp[(i-1)%2][j-1]+1; else dp[i%2][j]=max(dp[(i-1)%2][j],dp[i%2][j-1]); } } printf("%d\n",n-dp[n%2][n]); } return 0; }
0 0
- LCS 及LCS+滚动数组 模板
- HDU1513(LCS+滚动数组)
- POJ1159Palindrome【LCS+滚动数组】
- LCS滚动数组解法
- hdu_1513_Palindrome(LCS+滚动数组)
- 【HDU】-1513-Palindrome(LCS,模板+滚动数组)
- poj1159-LCS滚动数组优化。
- (1513)HDU-lcs+滚动数组
- HDU1513 Palindrome(LCS+滚动数组)
- HDOJ 1513 LCS + 滚动数组
- hdu 1159 滚动数组+LCS
- HDU 1513 LCS+滚动数组
- Palindrome POJ1159 LCS+滚动数组
- LCS模板
- LCS模板
- lcs 模板
- LCS模板
- lcs模板
- WebSocket协议深入理解
- HDU1051:Wooden Sticks
- VideoView学习笔记
- java SE基础部分 点
- ACDream 1734 Can you make a water problem?(贪心)
- LCS 及LCS+滚动数组 模板
- UVA - 705 Slash Maze
- 原型相关
- UVA - 439 Knight Moves
- C#中哈希表(HashTable)的用法详解
- 创建文件和删除文件
- Android PopupWindow详解
- Color the ball(树状数组)
- Python2.6+PyQt4.8+eric4+汉化+开发教程