zoukankan      html  css  js  c++  java
  • 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");
    	          } 
               }     
         }  
     }     


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    python正则表达式
    pyperclip模块
    python画国旗
    linux6.5禁用telnet
    限制用户su到root
    按系统日期生成目录
    微软windows logo配色rgb
    fastclick使用与 fastclick ios11.3相关bug原因(ios输入框点击变得不灵敏,ios input失焦后,页面上移,点击不了)
    vue iframe嵌套页面高度自适应 (ios 宽度扩大的bug , ios展示比例问题)
    iOS设备 微信h5页面回退 内容不刷新的问题
  • 原文地址:https://www.cnblogs.com/AndyDai/p/4734153.html
Copyright © 2011-2022 走看看