poj1458 Common Subsequence ——最长公共子序列

August 17, 2013
POJ DP

link:http://poj.org/problem?id=1458 最基础的那种


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
using namespace std;
int sad[1111][1111];char apple[1111],pear[1111];
int main(void)
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin );
    #endif // ONLINE_JUDGE
//    while (gets(apple)!=NULL && gets(pear)!=NULL)
    while (~scanf("%s%s",apple,pear))
    {
        memset(sad,0,sizeof(sad));
        int lena=strlen(apple),lenp=strlen(pear);
        for(int i=0;i<lena;++i)
        {
            for(int j=0;j<lenp;++j)
            {
                if(apple[i]==pear[j]) sad[i+1][j+1]=sad[i][j]+1;
                else sad[i+1][j+1]=max(sad[i][j+1],sad[i+1][j]);
            }
        }
        printf("%d\n",sad[lena][lenp]);
    }

    return 0;
}

开始把sad数组类型写成char型了,样例竟然也会对。

comments powered by Disqus