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)

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");
	          } 
           }     
     }  
 }     


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值