HDU 1159 Common Subsequence (DP seeking LCS)

Source: Internet
Author: User

Common subsequenceTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 24489 Accepted Submission (s): 10823


Problem Descriptiona 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 E Xists a strictly increasing sequence <i1, I2, ..., ik> of indices of X such so for all J =,..., K, Xij = ZJ. For example, Z = <a, B, F, c> are a subsequence of X = <a, B, C, F, B, c> with index sequence <1, 2, 4, 6&gt ;. Given sequences X and y the problem is to find the length of the Maximum-length common subsequence of x and Y.
The program input was from a text file. Each data set in the file contains the strings representing the given sequences. The sequences is separated by any number of white spaces. The input data is correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from th e beginning of a separate line.

Sample Input
ABCFBC abfcabprogramming contest ABCD MNP

Sample Output
420
#include <stdio.h> #include <string> #include <vector> #include <iostream>using namespace std;    int main (int argc, char *argv[]) {string A, B;        while (cin>>a>>b) {vector<vector<int> > C;        int x=a.size ();        int y=b.size ();        int size=x>y?x:y;        Size+=1;        C.resize (SIZE);        for (int i=0;i<size;++i) c[i].resize (size,0);        for (int i=0;i<=x;++i) c[i][0]=0;        for (int i=0;i<=y;++i) c[0][i]=0;                for (int i=1;i<=x;++i) for (int j=1;j<=y;++j) {if (a[i-1]==b[j-1])                {c[i][j]=c[i-1][j-1]+1;                } else {c[i][j]=c[i][j-1]>c[i-1][j]?c[i][j-1]:c[i-1][j];    }} cout<<c[x][y]<<endl; } return 0;}



Hdu 1159 Common Subsequence (DP seeking LCS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.