hdu 1159 dp LCS

这题的话,边界处理起来比较复杂,我归纳了讨论,最后还是wa了,哎,看了dicuss的代码,处理还是巧妙

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
const int MAXN = 1E3+10;

int dp[MAXN][MAXN];

int main()
{
    string s1,s2;
    while(cin>>s1>>s2){
        memset(dp,0,sizeof(dp));
        int n1 = s1.length();
        int n2 = s2.length();

        for(int i=1;i<=n1;i++){
            for(int j=1;j<=n2;j++){
                if(s1[i-1]==s2[j-1]){
                    dp[i][j] = dp[i-1][j-1]+1;
                }else{
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
       cout<<dp[n1][n2]<<endl;
    }
    return 0;
}

时间: 2024-10-18 17:38:09

hdu 1159 dp LCS的相关文章

hdu 1159(DP+字符串最长公共序列)

http://blog.csdn.net/a_eagle/article/details/7213236 公共序列可以用一个二维数组dp[i][j]保存每个点时的最大数字,本质就是一个双向比较. dp[i][j] = dp[i-1][j-1]+1;(a[i]==b[j]) dp[i][j] = max(dp[i][j-1],dp[i-1][j]);(a[i]!=b[j]) 处理字符串时可以用sf("%s",a+1)来将字符串从1开始保存,这时strlen()也要用a+1 #includ

HDU 1159——Common Subsequence(DP)

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23279    Accepted Submission(s): 10242 Problem Description A subsequence of a given sequence is the given sequence with some e

POJ 1159 Palindrome &amp;&amp; HDU 1159 Common Subsequence

1.先说说杭电的1159吧! 这道题是基础动规,比较简单! 就是要你求最长的公共子序列(不要优化) 动态转移方程: dp[i+1][j+1]=(a[i]=b[i])?dp[i][j]+1:max(dp[i+1][j],dp[i][j+1]) AC代码: #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 520 char a[N],b[N]; in

poj 1159 Palindrome lcs+滚动数组

点击打开链接题目链接 Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52910   Accepted: 18248 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are

poj 1159 Palindrome (LCS)

链接:poj 1159 题意:给定一个字符串,求最少添加多少个字符可使得该字符串变为回文字符串 分析:设原序列S的逆序列为S' ,最少需要补充的字母数 = 原序列S的长度 - S和S'的最长公共子串长度 原因:要求最少添加几个字符,我们可以先从原串中找到一个最长回文串,然后对于原串中不属于这个回文串的字符,在它关于回文串中心的对称位置添加一个相同字符即可.那么需要添加的字符数量即为n-最长回文串长度. 最长回文串可以看作是原串中前面和后面字符的一种匹配(每个后面的字符在前面找到一个符合位置要求的

HDU 1159:Common Subsequence(最长公共子序列)

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23108    Accepted Submission(s): 10149 Problem Description A subsequence of a given sequence is the given sequence with some e

HDU 4832(DP+计数问题)

HDU 4832 Chess 思路:把行列的情况分别dp求出来,然后枚举行用几行,竖用几行,然后相乘累加起来就是答案 代码: #include <stdio.h> #include <string.h> #include <iostream> using namespace std; typedef long long ll; const ll MOD = 9999991; const int N = 1005; int t, n, m, k, x, y; ll dp1

hdu 1159

题意:求最长子序列 #include<iostream>#include<cstring>using namespace std;int max(int x,int y){    if(x>y) return x;    return y;}int map[1001][1001];char s1[1001],s2[1001];int main(){        while(cin>>s1+1>>s2+1)    {        memset(map

poj1458——dp,lcs

poj1458——dp,lcs Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40529   Accepted: 16351 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence