CF - 223 - B. Two Strings

题意:给出两个由小写字母组成的字符串s和t(长度都不超过2*10^5),问能否用t作为s的子序列把s的每一个字母都用过(s中的字母可以用多次)。

题目链接:http://codeforces.com/problemset/problem/223/B

——>>参考wuyiqi的想法:http://blog.csdn.net/crazy_ac/article/details/7999607微笑

第一步:从左往右扫描s,记录s[i]能匹配到t的最右位置L[i];

第二步:从右往左扫描s,记录s[i]能匹配到的t最左位置R[i];

以上两步,根据字母去找,如果一个字母已经匹配到了某个位置,那么以后这个字母至少可以匹配到这个位置。

第三步:扫描判断L与R,如果L[i],R[i]匹配不上,或者L[i] < R[i](此时说明s中不存在包含s[i]的子序列匹配t,假设存在,即使把s[i]分成两个,一个匹配t的左边到L[i],一个匹配t的右边到R[i],但是t中L[i] < < R[i]的位置匹配不上,假设不成立)。

#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 200000 + 10;

char s[maxn], t[maxn];
int L[maxn], R[maxn], last[maxn];

int main()
{
    while(scanf("%s%s", s, t) == 2) {
        memset(last, -1, sizeof(last));
        int j = 0, slen = strlen(s), tlen = strlen(t);
        for(int i = 0; i < slen; i++) {
            L[i] = last[s[i]-'a'];
            if(j < tlen && s[i] == t[j]) {
                L[i] = j;
                j++;
            }
            last[s[i]-'a'] = L[i];
        }
        memset(last, -1, sizeof(last));
        j = tlen - 1;
        for(int i = slen-1; i >= 0; i--) {
            R[i] = last[s[i]-'a'];
            if(j >= 0 && s[i] == t[j]) {
                R[i] = j;
                j--;
            }
            last[s[i]-'a'] = R[i];
        }
        bool ok = true;
        for(int i = 0; i < slen; i++)
            if(L[i] == -1 || R[i] == -1 || L[i] < R[i]) {
                ok = false;
                break;
            }
        ok ? puts("Yes") : puts("No");
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
checking whether the compiler supports GNU C++... yes checking whether g++ accepts -g... yes checking for g++ option to enable C++11 features... none needed checking dependency style of g++... gcc3 checking how to run the C preprocessor... gcc -std=gnu11 -E checking for x86_64-w64-mingw32-ranlib... no checking for ranlib... ranlib checking for x86_64-w64-mingw32-dlltool... no checking for dlltool... no checking for x86_64-w64-mingw32-ar... no checking for x86_64-w64-mingw32-lib... no checking for x86_64-w64-mingw32-link... no checking for ar... ar checking the archiver (ar) interface... ar checking dependency style of gcc -std=gnu11... gcc3 checking for x86_64-w64-mingw32-as... no checking for as... as checking whether dlltool supports --temp-prefix... yes checking whether to build a w32api package for Cygwin... no checking whether to build the Win32 libraries... yes checking whether to build the Win64 libraries... yes checking whether to build the WinARM32 libraries... no checking whether to build the WinARM64 libraries... no checking whether to use genlib... no checking whether to enable globbing... no checking whether to enable private exports... no checking whether to enable delay import libs... no checking what to provide as libmsvcrt.a... msvcrt-os checking whether to include support for Control Flow Guard... no checking whether to enable experimental features... no checking whether the compiler supports -municode... no checking for stdio.h... yes checking for stdlib.h... yes checking for string.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for strings.h... yes checking for sys/stat.h... yes checking for sys/types.h... yes checking for unistd.h... yes checking for _mingw_mac.h... no
最新发布
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值