• 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;
    }
  • 相关阅读:
    Kubernetes+Docker+Istio 容器云实践
    SDN网络IPv6组播机制支持实时视频业务海量用户扩展
    如何运用结构化思维进行故障处理
    UAVStack JVM监控分析工具:图形化展示采集及分析监控数据
    个性化推荐产品功能的设计和B端产品的功能策划方式
    Kubernetes监控实践(2):可行监控方案之Prometheus和Sensu
    demo演示如何写一个无配置格式统一的日志
    指尖前端重构(React)技术调研分析
    学习设计模式前传
    我是码农要翻身
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766381.html
Copyright © 2020-2023  润新知