zoukankan      html  css  js  c++  java
  • poj 3302 Subsequence

    题目链接:http://poj.org/problem?id=3302

            题意:  给定两个字符串,求第二个字符串是否为第一个字符串正向或反向的子串。

            直接按s2中的字符在s1中搜索,有两个地方要剪枝,一是当s2的长度大于s1时,还有当s1剩余字符无s2的当前字符时。

    #include<cstdio>
    #include
    <fstream>
    #include
    <iostream>
    #include
    <cstring>
    using namespace std ;
    int n, i, j ;
    char s1[101], s2[101] ;
    bool is_sub(){
        
    int s1_len = strlen(s1) ;
        
    int s2_len = strlen(s2) ;
        
    int sn=0, t=0 ;
        
    int find=0 ;
        
    if(s2_len>s1_len)   return false ;  //剪枝
        
    for(i=0; i<s2_len; i++){            //正向
            find=0 ;
            
    for(j=t; j<s1_len; j++){
                
    if(s1[j]==s2[i]){
                    sn 
    ++ ;
                    t 
    = j+1 ;
                    find 
    = 1 ;
                    
    break ;
                }
            }
            
    if(sn==s2_len)  return true ;
            
    else if(find==0)    break ;     //剪枝
        }
        sn
    =0 ;
        t
    =0 ;
        
    for(i=s2_len-1; i>=0; i--){         //反向
            find=0 ;
            
    for(j=t; j<s1_len; j++){
                
    if(s1[j]==s2[i]){
                    sn 
    ++ ;
                    t 
    = j+1 ;
                    find 
    = 1 ;
                    
    break ;
                }
            }
            
    if(sn==s2_len)  return true ;
            
    else if(find==0)    break ;
        }
        
    return false ;
    }
    int main(){
        fstream cin(
    "input.in") ;
        cin 
    >> n ;
        
    while(n--){
            cin 
    >> s1 >> s2 ;
            
    if(is_sub())
                cout 
    << "YES" << endl ;
            
    else
                cout 
    << "NO" << endl ;
        }
        
    return 0 ;
    }
  • 相关阅读:
    apache commons-io相关介绍-IOUtils类
    apache commons-io相关介绍-DirectoryWalker类
    apache commons-io相关介绍-FileUtils类
    apache commons-io相关介绍-monitor包
    android笔记--AsyncTask例子
    Java Swing中的SwingWorker
    Predicting Boston Housing Prices
    matplotlib.pyplot 中很好看的一种style
    机器学习算法比较
    udacity 机器学习课程 project2
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2173831.html
Copyright © 2011-2022 走看看