Codeforces Round #138 (Div. 1)B. Two Strings

本文解析了Codeforces 223B题目的解决方案,探讨了如何在给定的字符串S中找到所有与字符串T相匹配的子序列,并确保S中的每个字符至少出现在一个匹配的子序列中。文章详细解释了使用map数据结构记录匹配过程的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

codeforces.com/problemset/problem/223/B

B. Two Strings

A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 ≤ k1 < k2 < ... < k|x| ≤ |s|).

You've got two strings — s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at least one of these subsequences? In other words, is it true that for all i (1 ≤ i ≤ |s|), there is such subsequence x = sk1sk2... sk|x| of string s, that x = t and for some j (1 ≤ j ≤ |x|) kj = i.

Input

The first line contains string s, the second line contains string t. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2·105.

Output

Print "Yes" (without the quotes), if each character of the string s occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise.

Examples

Input

Copy

abab
ab

Output

Copy

Yes

Input

Copy

abacaba
aba

Output

Copy

No

Input

Copy

abc
ba

Output

Copy

No

Note

In the first sample string t can occur in the string s as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string s occurs at least once.

In the second sample the 4-th character of the string s doesn't occur in any occurrence of string t.

In the third sample there is no occurrence of string t in string s.

题意:给两个字符串S和T,在S中找T这个样子的子序列,然后染色,问:能不能把S串都染上色

这题对我来说真难理解,问大佬们都问了好几次。。。。
卡这道题卡了好几天了,心情真郁闷,不够好在现在有点理解了~~~

用一个map记录一哈S串中的每个字符作为最后一个字能匹配到T串中最长到哪里

然后就是我比较难以理解的了:
比如说
abca
abc
当看 s[4]=‘a’ 的时候
这个时候Mp[‘a’]=1
说明 ‘a’ 这个字符才匹配到T串中的第一个字,那说明不行,后面必须有bc来匹配才行

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EXP exp(1)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int maxn=200005;
char s1[maxn],s2[maxn];
int main()
{
    scanf(" %s %s",s1+1,s2+1);
    //printf("%s\n%s\n",s1+1,s2+1);
    int N1=strlen(s1+1);
    int N2=strlen(s2+1);
    map<char,int> Mp;//用一个map记录一哈S串中的每最后个字符作为一个字符能匹配到T串中最长到哪里
    int now=1;//表示这个字符作为最后一个字符能匹配T串最多到哪里
    int t=1;//用来看是不是匹配到最后了
    for(int i=1; i<=N1; i++)
    {
        if(s1[i]==s2[now])//表示s1[i]已经匹配到了
            Mp[s1[i]]=now++;
        if(s1[i]==s2[t])//t表示的是当前匹配的s2串的位置
            t++;
        //如果Mp[s[i]]比t小,那就说明又要从前面一点开始匹配,即表示没有之前没有能过匹配到t位置的00
        t=min(t,Mp[s1[i]] ? Mp[s1[i]]+1:0);  //这个字符出现过的话就+1,没有出现需要从头开始匹配
        //有一个不能匹配的,t就等于0,表示不能匹配
    }
    if(t<=N2)
        puts("No");
    else
        puts("Yes");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值