gavanwanggw

导航

hdu5414(2015多校10)--CRB and String(字符串匹配)

题目链接:点击打开链接

题目大意:有A。B两个字符串。如今有一种操作能够在A的随意一个字符x后面添加一个字符y(x。=y)。问能不能将A变为B。

首先假设A能够变成B,那么A就一定是B的一个子序列,这个能够在O(n+m)的时间内算出。

假设A是B的子序列之后,推断添加的字符中是不是含有不能添加的情况,我们仅仅须要推断B从開始的一段连续的同样的字符串。是不是在A的开头也存在。假设存在,那么就是能够由A转化成B的。


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
char s1[100010] , s2[100010] ;
int main() {
    int t , i , j , l1 , l2 ;
    char ch ;
    //freopen("1009.in","r",stdin) ;
    //freopen("9.out","w",stdout) ;
    scanf("%d", &t) ;
    while( t-- ) {
        scanf("%s %s", s1, s2) ;
        l1 = strlen(s1) ;
        l2 = strlen(s2) ;
        i = j = 0 ;
        while( i < l1 && j < l2 ) {
            if( s1[i] == s2[j] ) {
                i++ ; j++ ;
            }
            else
                j++ ;
        }
        if( i < l1 ) {
            printf("No\n") ;
            continue ;
        }
        for(j = 1 ; j < l2 ; j++)
            if( s2[j] != s2[j-1] ) break ;
        for(i = 0 ; i < j ; i++)
            if( s1[i] != s2[0] ) break ;
        if( i < j )
            printf("No\n") ;
        else
            printf("Yes\n") ;
    }
    return 0 ;
}


posted on 2017-06-13 14:53  gavanwanggw  阅读(111)  评论(0编辑  收藏  举报