zoukankan      html  css  js  c++  java
  • HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence【DP】

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 44280 Accepted Submission(s): 20431

    Problem Description
    A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, …, xm > another sequence Z = < z1, z2, …, zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, …, ik > of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
    The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.

    Sample Input

    abcfbc abfcab
    programming contest
    abcd mnp

    Sample Output

    4
    2
    0

    题意

    求解两个字符串的最长公共子序列

    思路
    如果两个字符串的最后一个字符相等,那么由这最后一个字符组成的最长公共子序列就是 前面的最长公共子序列长度+ 1 然后往前推 就可以了
    DP[i][j] = DP[i - 1][j - 1] + 1
    如果不相等
    DP[i][j] = max(DP[i - 1][j], DP[i][j - 1])

    AC代码

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <cstring>
    #include <map>
    #include <stack>
    #include <set>
    #include <cstdlib>
    #include <ctype.h>
    #include <numeric>
    #include <sstream>
    using namespace std;
    
    typedef long long LL;
    const double PI = 3.14159265358979323846264338327;
    const double E = 2.718281828459;
    const double eps = 1e-6;
    const int MAXN = 0x3f3f3f3f;
    const int MINN = 0xc0c0c0c0;
    const int maxn = 1e3 + 5;
    const int MOD = 1e9 + 7;
    int dp[maxn][maxn];
    
    int main()
    {
        string a, b;
        while (cin >> a >> b)
        {
            int len_a = a.size(), len_b = b.size();
            memset(dp, 0, sizeof(dp));
            LL ans = 0;
            for (int i = 0; i < len_a; i++)
            {
                if (b[0] == a[i])
                {
                    dp[i][0] = 1;
                    ans = 1;
                }
                else if (i)
                    dp[i][0] = dp[i - 1][0];
            }
            for (int i = 0; i < len_b; i++)
            {
                if (a[0] == b[i])
                {
                    dp[0][i] = 1;
                    ans = 1;
                }
                else if(i)
                    dp[0][i] = dp[0][i - 1];
            }
            for (int i = 1; i < len_a; i++)
            {
                for (int j = 1; j < len_b; j++)
                {
                    if (a[i] == b[j])
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                    else
                        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
            cout << dp[len_a - 1][len_b - 1] << endl;
        }
    }
  • 相关阅读:
    C#与数据库访问技术总结(十四)之DataAdapter对象
    《运维三十六计》:运维生存宝典
    企业运维几百个重点面试题汇总(老男孩)
    5、KVM虚拟化典型案例:生产环境问题案例与分析
    Linux运维必会的实战编程笔试题(19题)
    面试中常见的 MySQL 考察难点和热点
    Linux系统运维常见面试简答题(36题)
    一键备份脚本 backup.sh
    Linux常用的200个命令总结分类
    2、KVM虚拟化CPU、内存、网络、磁盘技术及性能调优方法
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433375.html
Copyright © 2011-2022 走看看