Poj 1936,3302 Subsequence(LCS)
一、Description(3302)
Given a string s of length n, a subsequence of it, is defined as another string
s' = su1su2...sum where 1 ≤
u1 < u2 < ... < um ≤
n and si is the ith character of s. Your task is to write a program that, given two strings
s1 and s2, checks whether either s2 or its reverse is a subsequence of
s1 or not.
Input
The first line of input contains an integer T, which is the number of test cases. Each of the next
T lines contains two non-empty strings s1 and s2 (with length at most 100) consisted of only alpha-numeric characters and separated from each other by a single space.
Output
For each test case, your program must output "YES", in a single line, if either
s2 or its reverse is a subsequence of s1. Otherwise your program should write "NO".
一、Description(1936)
are generated and inserted into the original message. To validate your method, however, it is necessary to write a program that checks if the message is really encoded in the final string.
Given two strings s and t, you have to decide whether s is a subsequence of t, i.e. if you can remove characters from t such that the concatenation of the remaining characters is s.
Input
Output
二、题解
这两道题目都是求LCS问题,只是求解的形式有所不同。求的不是LCS而是判断是否为子序列,其中3302还求逆序列是否为子序列。但不管怎么变,只要记住DP求LCS算法,题目就很容易求解。
三、java代码
import java.util.Scanner;
public class Main {
public static int LCS(String x,String y){
int [][] z=new int [x.length()+1][y.length()+1];
int i,j;
for( i=0;i<=x.length();i++)
z[i][0]=0;
for( j=0;j<=y.length();j++)
z[0][j]=0;
for(i=1;i<=x.length();i++){
for( j=1;j<=y.length();j++){
if(x.charAt(i-1)==y.charAt(j-1)){
z[i][j]= z[i-1][j-1]+1;
}
else
z[i][j]=z[i-1][j] > z[i][j-1] ?z[i-1][j]:z[i][j-1];
}
}
return z[x.length()][y.length()];
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String s,s1;
int n,i;
n=cin.nextInt();
for(i=0;i<n;i++){
s=cin.next();
s1=cin.next();
if( LCS(s,s1)==s1.length() || LCS(s,new StringBuffer(s1).reverse().toString())==s1.length()){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Poj 1936,3302 Subsequence(LCS)的更多相关文章
- Poj 1458 Common Subsequence(LCS)
一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...
- LCS POJ 1458 Common Subsequence
题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...
- POJ 1458 Common Subsequence(LCS最长公共子序列)
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- (线性dp,LCS) POJ 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65333 Accepted: 27 ...
- POJ - 1458 Common Subsequence DP最长公共子序列(LCS)
Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...
- POJ 1458 Common Subsequence (zoj 1733 ) LCS
POJ:http://poj.org/problem?id=1458 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=73 ...
- POJ 1458 Common Subsequence 最长公共子序列 LCS
LCS #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> ...
随机推荐
- nginx中使用waf防火墙
1.安装依赖 yum install -y readline-devel ncurses-devel 2.安装Lua # .tar.gz # cd lua- # make linux # make i ...
- Is this its limit?
import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(c ...
- The space of such functions is known as a reproducing kernel Hilbert space.
Reproducing kernel Hilbert space Mapping the points to a higher dimensional feature space http://www ...
- Image Recognition
https://www.tensorflow.org/tutorials/image_recognition
- vue 计算属性和监听器
一.计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div> {{ message.split('').rev ...
- awk 字符串函数
awk 提供了许多强大的字符串函数,见下表: awk 内置字符串函数 gsub(r,s) 在整个 $0 中用 s 替代 r gsub(r,s,t) 在整个 t 中用 s 替代 r index(s,t) ...
- docker swarm部署spring cloud服务
一.准备docker swarm的集群环境 ip 是否主节点 192.168.91.13 是 192.168.91.43 否 二.准备微服务 ①eureka服务 application.y ...
- iOS 图文混排 链接 可点击
对于这个话题 我想到 1 第一个解决方法就是使用 webView 比较经典 把所有复杂工作都交给控件本身去处理了, 但是好像好多需要自定义的地方 没法从 webView获得响应回调 :(估计也可以实 ...
- leetcode 901. Online Stock Span
Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...
- 第四章 python中的面向对象设计
一.首先来理解几个面向对象的关键特性: 1.封装:对象可以将他们的内部状态隐藏起来.python中所有特性都是公开可用的. 2.继承:一个类可以是一个或多个类的子类.python支持多重继承,使用时需 ...