zoukankan      html  css  js  c++  java
  • ACM程序设计选修课——1018: Common Subsequence(DP)

    问题 L: Common Subsequence

    时间限制: 1 Sec  内存限制: 32 MB
    提交: 70  解决: 40
    [提交][状态][讨论版]

    题目描述

    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.

    输入

     

    输出

     

    样例输入

    abcfbc abfcab
    programming contest 
    abcd mnp

    样例输出

    4
    2
    0

    ..在大神的指点下,似乎明白了点啥。智商需要充值。假设这俩字符串为a与bC[i][j]来表示当前移动到a的第i个位置,b的第j个位置时所产生的最大公共子字符个数。

    C[i][j]要么保持上两个状态中的一个,要么出现了公共的字符进行+1操作。

    list[i+1][j+1]=(a[i]==b[j] ? list[i][j]+1 : max( list[i+1][j] , list[i][j+1]) );

    代码:

    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<map>
    #include<set>
    #include<cstring>
    #include<cmath>
    using namespace std;
    int list[1010][1010];
    int main(void)
    {
    	int t,i,j,ans;
    	string a,b;
    	while (cin>>a>>b)
    	{
    		memset(list,0,sizeof(list));
    		int la=(int)a.size();
    		int lb=(int)b.size();
    		for (i=0; i<la; i++)
    		{
    			for (j=0; j<lb; j++)
    			{
    					list[i+1][j+1]=(a[i]==b[j]?list[i][j]+1:max(list[i+1][j],list[i][j+1]));//状态转移方程
    			}
    		}
    		printf("%d
    ",list[la][lb]);
    	}
    	return 0;
    }
  • 相关阅读:
    DateTimeHelper
    Check F5 refresh submit post back repeatly
    GZip Compress SoapExtension
    WebBrowser 高级扩展 js扩展 js订阅C#事件
    DataSet GZip/Deflate Serializer
    Socket/Stream Data Helper
    历年评书出版一览表(1955~1994)
    c#检测字节流编码
    Execute SQL Server Store Procedure C# ADO.Net Wrapper Code Generator
    Directory File Disk Searcher
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766381.html
Copyright © 2011-2022 走看看