HDU 1159 Common Subsequence
2015-08-25 15:40
423 查看
原题链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1159题目大意:
给你两个字符串。找出他们的最大公共字串思路:
逐个匹配。并用DP数组记录下。到这个位置最多有多少个字符匹配具体想法看代码注释
代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; char str1[500],str2[500];//用来存放两个字串 int dp[500][500]; int main() { while( scanf("%s %s",str1,str2) != EOF ) { int len1 = strlen( str1 ); int len2 = strlen( str2 ); int i,j; //补齐空行,便于后面的代码操作 //第一个串竖放,第二个串横放 for( i = 0; i <= len1; i++ ) dp[i][0] = 0; for( j = 0; j <= len2; j++ ) dp[0][j] = 0; //逐个匹配 for( i = 0; i < len1; i++ ) for( j = 0; j < len2; j++ ) { //如果当前项匹配。那么就把前一项已经匹配好的最大值+1赋值给当前的DP就可以了 if( str1[i] == str2[j] ) dp[i+1][j+1] = dp[i][j] + 1; //如果不匹配。那么这个位置DP的值就为。从(第一个串)i位置开始匹配到的个数多,还是(第二个串)j位置在之前的位置匹配的多 else dp[i+1][j+1] = max( dp[i+1][j],dp[i][j+1] ); } printf("%d\n",dp[len1][len2]); } return 0; }
相关文章推荐
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android px、dp、sp之间相互转换
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题
- LeetCode之Maximum Product Subarray
- DP Flow
- zoj3605 Find the Marble(三维dp)
- Word Break I,II, Triangle,Palindrome Partitioning 动态规划 DP
- Unique Paths I,II, Minimum Path Sum