HDU Common Subsequence (dp)

Common Subsequence

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 14   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

Source

Southeastern Europe 2003

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int main(){
    string s1,s2;
    int dp[500][500];
    int i,j,lena,lenb;
    while(cin>>s1>>s2){
        lena=s1.length();
        lenb=s2.length();
        for(i=0;i<lena;++i)dp[0][i]=0,dp[i][0]=0;
        for(i=0;i<lena;++i){
            for(j=0;j<lenb;++j){
                if(s1[i]==s2[j]){
                    dp[i+1][j+1]=dp[i][j]+1;
                }
                else{
                    dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
                }
                //printf("dp[%d][%d]=%d ",i,j,dp[i][j]);
            }
           // putchar('\12');
        }
        printf("%d\n",dp[lena][lenb]);
    }
    return 0;
}
时间: 2024-08-06 05:40:11

HDU Common Subsequence (dp)的相关文章

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

Uva 10405-Longest Common Subsequence(DP)

题目链接:点击打开链接 裸LCS 注意输入可能有空格 #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #inclu

HDU 4832 Chess (DP)

Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24    Accepted Submission(s): 10 Problem Description 小度和小良最近又迷上了下棋.棋盘一共有N行M列,我们可以把左上角的格子定为(1,1),右下角的格子定为(N,M).在他们的规则中,"王"在棋盘上的走法遵循十字路线.

HDU 2845 Beans (DP)

Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want to eat the beans and collect the qualities, but everyo

HDU 4945 2048(dp)

题意:给n(n<=100,000)个数,0<=a[i]<=2048 .一个好的集合要满足,集合内的数可以根据2048的合并规则合并成2048 .输出好的集合的个数%998244353 . 比赛的时候想着1跟3可以合并成4 ....然后就越搞越复杂了.....2048玩得不多的我没有透彻的合并规则概念..... 看了题解写了发,妥妥地TLE...本地随意n=100,000都TLE了... 用dp[i][j]表示当前 i个2^j 的方案数 然后队友提醒优化,就是,当枚举到比如,value =

hdu 1300 Pearls (dp)

题目大意: 多种珍珠,每次选购都要在原有的数量上加上10. 例如:买5个单价是10的珍珠.需要的花费是(5+10)*10= 150.买100个单价是20的珍珠 需要的花费是(100+10)*20= 2200.总共需要的花费是150+2200=2350.如果把珍珠的质量提高了.需要的105个 珍珠都买单价是20的.也就是说都买质量好的.总的花费是(5+100+10)*20= 2300.在两组数据看来.珍珠都 买了高品质的了,而且花费也少了! 问题是怎么样能花费最少买珍珠! 思路分析: dp [i]

hdu 1159 Common Subsequence(dp 最长公共子序列问题LCS)

最长公共子序列问题(LCS,Longerst Common Subsequence). s1s2……si+1和t1t2……tj+1的公共子序列可能是: ①当si+1=tj+1时,在s1s2……si+1和t1t2……tj+1的公共子序列末尾追加一个. ②s1s2……si+1和t1t2……tj的公共子序列 ③s1s2……si和t1t2……tj+1的公共子序列 所以易得到递推关系dp[i+1][j+1]=  max{ dp[i][j]+1 , dp[i][j+1] , dp[i+1][j]) }  

hdu 1159 Common Subsequence (dp求LCS)

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

hdu 1159 Common Subsequence (dp乞讨LCS)

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