码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5414 CRB and String(想法题)

时间:2015-08-21 21:36:10      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:hdu   5414   

题意:

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

解析:

不要想太复杂了,分情况讨论清楚就好了。
1. 如果|s|>|t|,那么无论怎么组合都是无法构成t的。
2. 如果s[0]!=t[0],无论怎么组合,也都是无法构成t的。
3. 以上两种情况比较好考虑,然后找两个串的最长公共前缀。
如果s的最长公共前缀等于|t|则表明两个字符串完全相同,这种看情况可以构造出t,因为上面已经排除了|s|>|t|的情况了。
4. 由于插在后面的字符不能和其前面的字符相同,那么判断一下两个字符串的公共前缀,是不是全部都是一个字母,如果全部是一个字母,那么如果当前要插入的字符是和前面字符相同,那么就无法构造出t。
5. 排除了4这种情况,那么就可以任意构造出想要的字符串了,因为如果不同,就可以随意构造后面的字符,相同的话,就可以在前面不同的字符后面插入一个字符,等于构造出一个相同的字符。
那么相同的话两个指针一同向前推进,否则t的指针向前推进,判断最后s是否有剩余。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
const int N = (int)1e5 + 10;
char pat[N], tar[N];

bool judge() {
    int lenP = strlen(pat), lenT = strlen(tar);
    if(pat[0] != tar[0] || lenP > lenT)
        return false;

    int i = 0, j = 0;
    bool diff = false;
    while(i < lenP) {
        if(pat[i] == tar[j])
            i++, j++;
        else break;
        if(i > 1 && pat[i-1] != pat[i-2])
            diff = true;
    }

    if(i == lenT) return true;
    if(!diff && tar[j] == tar[j-1])
        return false;

    while(j < lenT) {
        if(pat[i] == tar[j]) i++;
        j++;
    }
    if(i == lenP) return true;
    return false;
}

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%s%s", pat, tar);
        puts(judge() ? "Yes" : "No");
    }
    return 0;
}

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

hdu 5414 CRB and String(想法题)

标签:hdu   5414   

原文地址:http://blog.csdn.net/helloworld10086/article/details/47839113

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!