题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&page=show_problem&problem=1346

Dynamic programming

需要注意的是input里可能含有空行,一行空行也是一个string,所以如果用scanf("%s", string)是不能把空行存进string变量里的。需要用gets或者getline来处理input。

可惜的是我目前对于gets,getline的用法还没有很熟悉,之后会关于这方面的区别做解释。又或者有大神可以帮忙讲解一下,感恩

代码如下:

 #include <iostream>
#include <math.h>
#include <stdio.h>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <cmath>
#define SCF(a) scanf("%d", &a)
#define IN(a) cin>>a
#define FOR(i, a, b) for(int i=a;i<b;i++)
typedef long long Int;
using namespace std; int main()
{
char str1[];
char str2[];
int len1, len2;
while (cin.getline(str1, ))
{
cin.getline(str2, );
for (len1 = ; str1[len1] != '\0'; len1++);
for (len2 = ; str2[len2] != '\0'; len2++);
int** match;
match = new int*[len1+];
FOR(i, , len1 + )
match[i] = new int[len2 + ];
FOR(i, , len1 + )
match[i][] = ;
FOR(i, , len2 + )
match[][i] = ; FOR(i, , len1 + )
{
FOR(j, , len2 + )
{
if (str1[i - ] == str2[j - ])
match[i][j] = match[i - ][j - ] + ;
else
{
match[i][j] = max(match[i - ][j], match[i][j - ]);
}
}
} printf("%d\n", match[len1][len2]);
FOR(i, , len1 + )
delete[] match[i];
delete[] match;
}
return ;
}

UVA 10405 Longest Common Subsequence的更多相关文章

  1. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  2. UVA 10405 Longest Common Subsequence --经典DP

    最长公共子序列,经典问题.算是我的DP开场题吧. dp[i][j]表示到s1的i位置,s2的j位置为止,前面最长公共子序列的长度. 状态转移: dp[i][j] = 0                 ...

  3. [UVa OJ] Longest Common Subsequence

    This is the classic LCS problem. Since it only requires you to print the maximum length, the code ca ...

  4. [Algorithms] Longest Common Subsequence

    The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...

  5. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  6. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  7. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  8. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  9. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

随机推荐

  1. 外购半成品报SHORT问题(验货客户)

    描述:下图中可以看到外购半成品层物料报SHORT 2.开始检查数据 --针对外购半成品(外购半成品的成品层有BOM数据,外购半成品没有BOM数据) '; --select * from TB_ADDB ...

  2. metasploit framework(六):信息收集

    nmap 扫描 扫描完毕之后,hosts查看扫描的结果 auxiliary 扫描 使用arpsweep模块扫描 查看设置 设置网卡和目标IP 设置伪造的源IP和源MAC set SHOST <伪 ...

  3. python使用函数作为参数

    在实际使用中,我们有时希望将函数作为参数传递给另一个方法使用. 比如装饰器实际就是函数调用函数   举个例子,我想传递在调用方法之前打印一下时间:   使用函数当做入参 那就可以把方法名A当做入参传递 ...

  4. 解决镜像无法删除的问题multiple repositories

    Error response from daemon: conflict: unable to delete ea5f89e79b1e (must be forced) - image is refe ...

  5. 153. Find Minimum in Rotated Sorted Array (Array; Divide-and-Conquer)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  6. Unity3D游戏贪吃蛇大作战源码休闲益智手机小游戏完整项目

    <贪吃蛇大作战>一款休闲竞技游戏,不仅比拼手速,更考验玩家的策略. 视频演示: http://player.youku.com/player.php/sid/XMzc5ODA2Njg1Ng ...

  7. The Attention Merchants

    Title: The Attention Merchants (书评类文章) <注意力商人> attention 注意力 merchant 商人(零售商,强调通过贩卖物品获取利益)  bu ...

  8. windows上安装Anaconda和python

    下载并安装 anaconda 先到https://www.continuum.io/downloads 下载anaconda, 现在的版本有python2.7版本和python3.5版本,下载好对应版 ...

  9. PTA 7-50 畅通工程之局部最小花费问题(最小生成树Kruskal)

    某地区经过对城镇交通状况的调查,得到现有城镇间快速道路的统计数据,并提出“畅通工程”的目标:使整个地区任何两个城镇间都可以实现快速交通(但不一定有直接的快速道路相连,只要互相间接通过快速路可达即可). ...

  10. FortiGate密码恢复

    1.需求 1.若设备的密码忘记,需要用配置线进行密码恢复: 2.密码恢复需要重启设备,并在设备的底层菜单界面上操作,会造成网络中断,请在方便断网时操作: 3.密码恢复后配置不会改变. 2.操作步骤 1 ...