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 1343:城际公路网 (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:445 解决:178 题目描述: 为了加快城市之间的通行和物资流动速度,A国政府决定在其境内的N个大中型城市之间,增加修建K条公路.已知这N个 ...
- linux c编程:进程控制(四)进程关系
每一个进程除了有一个进程ID外,还属于一个进程组. 进程组是一个或多个进程的集合,通常情况下,他们是在同一作业中结合起来的,同一进程组的个进程接受来自同一终端的各种信号. 每一个进程组有一个唯一的进 ...
- date_default_timezone_get():
[Symfony\Component\Debug\Exception\ContextErrorException] Warning: date_default ...
- java_Ninja实战过程
使用Ninja马上两年了,之前多多少少的都是跟着项目模仿着写,今年上半年准备从一个小项目开始从始至终走一遍; 首先官网:http://www.ninjaframework.org; github: h ...
- python基础10 ---匿名函数和递归
一.匿名函数 1.lambda表达式就相当于匿名函数,其格式为: lambda 参数列表:参数表达式 2.lambda自带return值,因为匿名函数有个限制,就是只能有一个表达式,不用写return ...
- hive查询注意及优化tips
Hive是将符合SQL语法的字符串解析生成可以在Hadoop上执行的MapReduce的工具.使用Hive尽量按照分布式计算的一些特点来设计sql,和传统关系型数据库有区别, 所以需要去掉原有关系型数 ...
- LeetCode:算法特辑——二分搜索
LeetCode:算法特辑——二分搜索 算法模板——基础 int L =0; int R =arr.length; while(L<R) { int M = (R-L)/2+L; if(arr[ ...
- 每天一个Linux命令(5)rm命令
rm命令可以删除一个目录中的一个或多个文件或目录,也可以将某个目录及其下属的所有文件及其子目录均删除掉.对于链接文件,只是删除整个链接文件,而原有文件保持不变. 注意:使用rm命令要格外小心.因为一旦 ...
- 《程序员代码面试指南》第三章 二叉树问题 Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题
题目待续.... Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题 java代码
- 【leetcode刷题笔记】Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...