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> ...
随机推荐
- 九度OJ 1186:打印日期 (日期计算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6366 解决:2214 题目描述: 给出年分m和一年中的第n天,算出第n天是几月几号. 输入: 输入包括两个整数y(1<=y<= ...
- java 分布式锁 -图解- 秒懂
目录 写在前面 1.1. 分布式锁 简介 1.1.1. 图解:公平锁和可重入锁 模型 1.1.2. 图解: zookeeper分布式锁的原理 1.1.3. 分布式锁的基本流程 1.1.4. 加锁的实现 ...
- Android系统移植与调试之------->MTK 标准编译命令
命令格式:./maketek [option] [project] [action] [modules]Option: -t ,-tee :输出log信息到当前终端 -o , -opt=-- ...
- 怎样将lua移植到arm平台的linux内核
将脚本移植到内核是一件非常酷的事情,lua已经被移植到NetBSD的内核中,也有一个叫lunatik的项目把lua移植到了linux内核.仅仅可惜仅仅支持x86.不支持arm,在网上搜索了下,没有找到 ...
- activiti踩坑
最近在学习activiti,偶然间遇到一个错误:加载引擎的时候报错,显示空指针错误,跟代码发现初始化配置文件返回为null.几经排查,可能是因为我发布流程后又清空了数据库数据导致的.然后我把表全部删除 ...
- 【HTTP】初识代理
Web代理(proxy)位于客户端和服务器端之间.HTTP的代理服务器既是Web服务器端又是Web客户端. 1. 代理和网关的对比 代理连接的是两个或者多个使用相同协议的应用程序. 网关连接的是两个或 ...
- ICCV 2015 B-CNN细粒度分类
哈哈,好久没写博客了....最近懒癌发作~~主要是因为心情不太好啊,做什么事情都不太顺心,不过已经过去啦.最近一直忙着公司的项目,想用这个网络,就给大家带来了的这篇文章.可能比较老,来自ICCV 20 ...
- windows中检查端口占用
在cmd中怎么输入netstat -aon|findstr "9080" 返回: UDP 0.0.0.0:8001 *.* 其中的4220为进城PID
- 从输入url到浏览器呈现网页发生了什么?
大致能分成两个部分:网络通信与页面渲染 一.网络通信 互联网各个网络设备间的通信均基于TCP/IP协议,此协议将整个过程进行分层,由上至下分别为: 应用层.传输层.网络层和数据链路层 1.输入URL ...
- 修改SecureCRT终端的Home和End功能键
SecureCRT真是个不错的ssh客户端工具,但在使用时发现跟自己的一些使用习惯不符合,例如home.end.pageup.pagedown和delete等键.默认情况下一些按键的功能如下:page ...