题目链接:
codeforces.com/problemset/problem/223/B
B. Two Strings
A subsequence of length | x| of string s?=? s1 s2... s| s| (where | s| is the length of string s) is a string x?=? s k1 s k2... s k| 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位置的00t=min(t,Mp[s1[i]] ? Mp[s1[i]]+1:0); //这个字符出现过的话就+1,没有出现需要从头开始匹配//有一个不能匹配的,t就等于0,表示不能匹配}if(t<=N2)puts("No");elseputs("Yes");return 0;
}