一、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)

You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way. Because of pending patent issues we will not discuss in detail how the strings
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

The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace.The length of s and t will no more than 100000.

Output

For each test case output "Yes", if s is a subsequence of t,otherwise output "No".

二、题解

        这两道题目都是求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)的更多相关文章

  1. Poj 1458 Common Subsequence(LCS)

    一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...

  2. LCS POJ 1458 Common Subsequence

    题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...

  3. POJ 1458 Common Subsequence(LCS最长公共子序列)

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

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

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

  5. POJ 1458 Common Subsequence(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  6. (线性dp,LCS) POJ 1458 Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65333   Accepted: 27 ...

  7. POJ - 1458 Common Subsequence DP最长公共子序列(LCS)

    Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...

  8. 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 ...

  9. POJ 1458 Common Subsequence 最长公共子序列 LCS

    LCS #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> ...

随机推荐

  1. iptables的用例

    iptables书写思路顺序 1.协议 icmp 2.哪个功能和目标:过滤,拒绝 3.数据包流向:外到内 4.哪个链适合:越早越好,INPUT 5.源地址和目标地址 练习1.禁止某些主机或网络访问本机 ...

  2. VS2017下编译iconv

    从http://www.gnu.org/software/libiconv/ 下载 libiconv-1.11.1, 这是最后一个支持MSVC编译的版本. 打开 Visual Studio 2017 ...

  3. C#DataSet/DataAdapter

    DataReader必须持续连接,所以在调用方法SqlDataReader作为返回类型时候,必须在方法外关闭流,很不方便. DataAdapter用于对数据源检索数据并填充到DataSet中的表.Da ...

  4. Yii2 如何更好的在页面注入CSS

    首先 先添加一个widgets,代码如下(提示:使用时注意修改命名空间) <?php /** * User: yiqing * Date: 2014/12/15 * Time: 0:21 */ ...

  5. 如何写PHP规范注释

    所有的文档标记都是在每一行的 * 后面以@开头.如果在一段话的中间出来@的标记,这个标记将会被当做普通内容而被忽略掉. @access        该标记用于指明关键字的存取权限:private.p ...

  6. iOS __weak 和 __block 的使用探讨

    在基本的开发中遇到 需要弱引用时候 我一般 用 weak  预防 死锁的时候 我会用 block 的确没出过大错  但是这样处理 的确有点囫囵  现在我想好好理解一下这两个修饰符 "bloc ...

  7. iOS NSDateFormatter 不安全线程 处理

    记得 上次我们开CodeReView大会 有人提出 "  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];   ...

  8. Executor中的类

    Executor框架 其中ThreadPoolExecutor非常重要,通过这个类自定义线程池 public ThreadPoolExecutor(int corePoolSize, //线程池里面的 ...

  9. python调用java jython

    环境:openjdk8,python2.7,jython2.7jython下载地址     http://www.jython.org/downloads.html 下载完成后,运行下面命令 java ...

  10. Python 3 接口与归一化设计

    一.接口与归一化设计: 1.归一化让使用者无需关心对象的类是什么,只需要知道这些对象都具备某些功能就可以了,这极大地降低了使用者的使用难度. 2.归一化使得高层的外部使用者可以不加区分的处理所有接口兼 ...