HDU 5414 CRB and String(贪心)

11 篇文章 0 订阅
9 篇文章 0 订阅

HDU 5414

题意:

输入两个字符串s和t,可以在字符串s中任意选一个字符c,在该字符c后插入一个字符d(d!=c),问经过多次此操作,能否将字符串s转化成字符串t。

思路:

我们思考题目所给条件:
显然 t 必须包含所有 s 的字符(s = “ab” , t = “acd”明显不可能);
其次插入字符d != c且向后插入,那么 s = “abc” t = “aabc”明显不可能,我们可以把这种情况归纳为:t 的前k个字符相同,则 s 必定前k个字符相同且与 t 一致。
所以按贪心的思路判断这两个条件即可:
①. t 包含 s 所有字符;
②. t 的前k个字符相同,则 s 必定前k个字符相同且与 t 一致。
参考:(http://blog.csdn.net/queuelovestack/article/details/47813785)

代码:

/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;

const int maxn = 100000 + 50 ;
char s[maxn] , t[maxn] ;
bool solve(){
    int i = 0 ;
    while( t[i] == t[i+1] ) i++;
    int j = 0 ;
    while( j <= i ){
        if( t[0] != s[j] ) return false ; //若字符串t前k个字符都相同,那么字符串s的前k个字符也必须相同
        j ++ ;
    }
    for( ; t[i] != '\0' ; i++ ){
        if( s[j] == t[i] ) j++ ;//s的所有字符都必须在t中出现.
    }
    return ( s[j] == '\0' && t[i] == '\0' ) ;
}

int main(){
    int kase ; cin >> kase ;
    while( kase-- ){
        scanf( "%s%s" , s , t ) ;
        if( solve() )
            puts( "Yes" ) ;
        else
            puts( "No" ) ;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值