HDU 5414 字符串

HDU 5414

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5414

题意:

给两个字符串S,T,问能否把S转换成T。转换规则是,可以在S中已知字母后插入新字母,但是新字母不能与已知字母相同。

思路:

先说错误思路,比赛的时候构图构错,把图想成把所有的s元素拆开,然后往里面加缝隙加元素或者不加。应该是相同的连一块,不同的元素之间拆开。

错误

 

鄙视     鄙视     鄙视     鄙视     鄙视     鄙视

 

正确

 鄙视鄙视    鄙视    鄙视鄙视    鄙视鄙视鄙视

 

 

 

这样就可以很轻易的发现,ST的第一块是要完全匹配的,后面的只要T在遍历到结尾前找到一个一样的,怎样都好(原因的话因为总可以在同一个元素后面反复插入元素来避免出现插入不合法的情况)。

源码:

#include <cstdio>

#include <cstring>

#include <cmath>

#include <cstdlib>

#include <string>

#include <algorithm>

#include <iostream>

using namespace std;

const int MAXN = 1e5 + 5;

char str1[MAXN], str2[MAXN];

bool solve()

{

    int len1 = strlen(str1);

    int len2 = strlen(str2);

    int i, j;

    for(j = 0 ; j < len2 ; j++)

        if(str2[j] != str2[0])

            break;

    for(i = 0 ; i < j ; i++)

        if(str1[i] != str2[i]){

//            printf("j = %d\n", j);

            return false;

        }

    for(; i < len1 ; i++){

        while(j < len2 && str1[i] != str2[j])

            j++;

        if(j == len2){

//            printf("i = %d\n", i);

            return false;

        }

    }

    return true;

}

int main()

{

    int t;

    scanf("%d", &t);

    while(t--){

        scanf("%s%s", str1, str2);

        int ok = solve();

        if(ok)

            printf("Yes\n");

        else

            printf("No\n");

    }

    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值