Fork me on GitHub

题目1042:Coincidence(最长公共子序列 dp题目)

题目链接:http://ac.jobdu.com/problem.php?pid=1042

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
//  1042 Coincidence.cpp
//  Jobdu
//
//  Created by PengFei_Zheng on 24/04/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#define MAX_SIZE 101
 
using namespace std;
 
char a[MAX_SIZE];
char b[MAX_SIZE];
int dp[MAX_SIZE][MAX_SIZE];
 
int main(){
    while(scanf("%s%s",a,b)!=EOF){
        int lena = (int)strlen(a);
        int lenb = (int)strlen(b);
        for(int i = 0 ; i <= lena ; i++){
            dp[i][0] = 0;
        }
        for(int j = 0 ; j <= lenb ; j++){
            dp[0][j] = 0;
        }
        for(int i = 1 ; i <= lena ; i++){
            for(int j = 1 ; j <= lenb ; j++){
                if(a[i-1]!=b[j-1]){
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                }
                else{
                    dp[i][j] = dp[i-1][j-1]+1;
                }
            }
        }
        printf("%d\n",dp[lena][lenb]);
    }
    return 0;
}
/**************************************************************
    Problem: 1042
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1560 kb
****************************************************************/

 

posted @ 2017-04-24 20:58  伊甸一点  阅读(218)  评论(0编辑  收藏  举报