[DP-LCS] POJ 1458
来源:互联网 发布:长江大数据交易所 编辑:程序博客网 时间:2024/06/05 19:36
题意
LCS
思路
LCS
代码
#include <cstdio>#include <cstring>#include <iostream>#define N 505using namespace std;int len1, len2;char s1[ N ], s2[ N ];int dp[ N ][ N ];inline int max ( int a, int b ) { return a > b ? a : b; }void LCS () { for ( int i = 1; i <= len1; i++ ) { for ( int j = 1; j <= len2; j++ ) { if ( s1[ i ] == s2[ j ] ) dp[ i ][ j ] = dp[ i - 1 ][ j - 1 ] + 1; else dp[ i ][ j ] = max ( dp[ i - 1 ][ j ], dp[ i ][ j - 1 ] ); } }}void Print ( int i, int j ) { //当最长的子序列搜索完,但其中一串仍有剩余时,输出 if ( i == 0 || j == 0 ) { return; } //找到公共字符 if ( s1[ i ] == s2[ j ] ) { Print ( i - 1, j - 1 ); printf ( "%c", s1[ i ] ); } else if ( dp[ i - 1 ][ j ] > dp[ i ][ j - 1 ] ) { Print ( i - 1, j ); } else { Print ( i, j - 1 ); }}int main () { while ( ~scanf ( "%s%s", s1 + 1, s2 + 1 ) ) { len1 = strlen ( s1 + 1 ); len2 = strlen ( s2 + 1 ); memset ( dp, 0, sizeof ( dp ) ); LCS (); printf ( "%d\n", dp[ len1 ][ len2 ] ); } return 0;}
阅读全文
0 0
- [DP-LCS] POJ 1458
- POJ 1458 Common Subsequence DP(LCS)
- POJ 1458 Common Subsequence (DP,LCS)
- 【DP|LCS】POJ-1458 Common Subsequence
- poj 1458 Common Subsequence (DP:LCS)
- Poj 1458 Common Subsequence【LCS】【dp】
- dp LCS poj 1458 Commom Subsequence
- poj 1458 LCS(dp)全部最长公共子序列
- POJ 1458-Common Subsequence(线性dp/LCS)
- DP-POJ 1458 (LCS 最长公共子序列)
- POJ 1458 LCS模板
- poj 1458 LCS
- POJ 1080 Human Gene Functions DP(LCS)
- poj 2250 Compromise dp lcs 路径输出
- POJ 1159-Palindrome(DP/LCS变形)
- poj 1159 Palindrome(lcs类似dp)
- poj 2250 Compromise(裸LCS DP)
- POJ 1080 Human Gene Functions(DP LCS)
- 输出GPLT(c语言)
- 购物车 Rrtrofit&RXjava
- 微信悄悄测试新功能,终于可以修改ID了!目测一大波妹纸正在改名路上
- 为什么函数重载不可以根据返回类型区分?
- httpClient服务访问报错406
- [DP-LCS] POJ 1458
- 购物车mvp
- 17练习
- Java将XML转换成Json格式数据
- win32开发(按键消息)
- 进程调度API之wait_for_completion_x
- Algorithm之路十五:3Sum
- 购物车
- 1-1 s3c2440 中断详解