UVA10192

来源:互联网 发布:电商与淘宝有什么区别 编辑:程序博客网 时间:2024/06/04 17:49

求最长公共子序列


AC代码:


#include<stdio.h>#include<string.h>#include<string>#include<iostream>using namespace std;const int N = 105;string str1 ,str2;int dp[N][N];int main () {int cas = 1;while (getline(cin , str1) && str1[0] != '#') {getline (cin , str2);memset(dp , 0 ,sizeof(dp));int len1 = str1.size();int len2 = str2.size();for (int i = len1; i >= 0 ;i--) {str1[i] = str1[i - 1];}for (int i = len2; i >= 0 ;i--) {str2[i] = str2[i - 1];}for(int i = 1 ; i <= len1 ;i++) {for (int j = 1  ;j <= len2 ;j++) {if (str1[i] == str2[j]) {dp[i][j] = dp[i - 1][j - 1] + 1;}else {dp[i][j] = dp[i - 1][j] > dp[i][j - 1] ? dp[i - 1][j] : dp[i][j - 1];}}}printf("Case #%d: ",cas++);printf("you can visit at most %d cities.\n",dp[len1][len2]);}}


0 0
原创粉丝点击