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;
    }
  • 相关阅读:
    org.apache.commons.io.FilenameUtils 常用的方法
    (转)同一服务器部署多个tomcat时的端口号修改详情
    JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用
    idea tomcat服务器运行打印日志到控制台是乱码解决方案
    spring boot 添加整合ssl使得http变成https方法
    Fiddler 抓包工具总结
    一些概念
    观点汇总
    Spring 问题总结
    tomcat和jetty区别
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766381.html
Copyright © 2011-2022 走看看