UVA 10405 Longest Common Subsequence
题目链接: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的更多相关文章
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
- UVA 10405 Longest Common Subsequence --经典DP
最长公共子序列,经典问题.算是我的DP开场题吧. dp[i][j]表示到s1的i位置,s2的j位置为止,前面最长公共子序列的长度. 状态转移: dp[i][j] = 0 ...
- [UVa OJ] Longest Common Subsequence
This is the classic LCS problem. Since it only requires you to print the maximum length, the code ca ...
- [Algorithms] Longest Common Subsequence
The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
随机推荐
- linux 后台运行命令
command & 关闭终端,程序会终止 nohup command & 关闭终端,程序不会终止
- javac编译带包的java文件需要在命令处加参数
不带包:javac aaa.java 带包:javac -d . aaa.java 带包就是 java文件中含有 package com.aaa;
- 查看linux发行版版本
可以通过查看/etc/issue文件查看发行版版本号,不论系统是redhat\suse\debian
- js导出excel:前端当前数据的导出
网上找的库文件,同样做了修改.在导出的时候,有时候数据第一列和最后一列可能是复选框和操作按钮,这个是我们不需要的,加了这个的过滤 //table2excel.js /* * jQuery table2 ...
- 《基于Nginx的中间件架构》学习笔记---1.环境配置
一.环境调试确认 (四项确认) 1.确认系统网络 ping www.baidu.com 2.确认yum可用 yum list|grep gcc 3.确认关闭iptables规则 iptables -L ...
- JQuery UI之Autocomplete(2)后端获取数据
1.Autocomplete获取后台数据 首先引入css和js文件,以及对应的HTML代码如下: <link href="../css/jquery-ui.css" rel= ...
- centos7下swoole1.9的安装与HttpServer的使用
一.下载swoole源码包 https://github.com/swoole/swoole-src/releases 如:swoole-src-1.9.6.tar.gz 二.编译安装 > yu ...
- c# 24种设计模式5原型模式(Prototype)
前言 原型模式其实C# Object中已经提供了一个Clone( )方法,平时很少用到,最近读Retrofit源码时候看到有这种使用方式. 定义 原型模式就是在系统clone()标记的基础上,对Clo ...
- 解决sublime3不能编辑插件default settings的问题
一.遇见问题 今天给sublime安装了View In Browser,想更改一下默认启动的浏览器 preferences-Package settings-View In Browser-setti ...
- Android——Activity跳转
Activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayou ...