题目描述:原题链接
You are given two strings s and t, both consisting of lowercase English letters. You are going to type the string s character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the “Backspace” button. It deletes the last character you have typed among those that aren’t deleted yet (or does nothing if there are no characters in the current string). For example, if s is “abcbd” and you press Backspace instead of typing the first and the fourth characters, you will get the string “bd” (the first press of Backspace deletes no character, and the second press deletes the character ‘c’). Another example, if s is “abcaa” and you press Backspace instead of the last two letters, then the resulting text is “a”.
Your task is to determine whether you can obtain the string t, if you type the string s and press “Backspace” instead of typing several (maybe zero) characters of s.
Input
The first line contains a single integer q (1≤q≤105) — the number of test cases.
The first line of each test case contains the string s (1≤|s|≤105). Each character of s is a lowercase English letter.
The second line of each test case contains the string t (1≤|t|≤105). Each character of t is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed 2⋅105.
Output
For each test case, print “YES” if you can obtain the string t by typing the string s and replacing some characters with presses of “Backspace” button, or “NO” if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
Example
input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
output
YES
NO
NO
YES
思路:
因为在前边的任意个字母可以一直按退格去掉,所以考虑将两字符串反转,便于从前往后遍历。如果t为偶数位置,说明前边已经处理好偶数个字符,每次不匹配需要去掉2个字符,所以偶加偶为偶,原串位置也应为偶数;如果t为奇数位置,同理,奇加偶为偶,所以原串位置也应为奇数。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int T; scanf("%d",&T);
while(T--)
{
string s,t;
cin>>s>>t;
reverse(s.begin(),s.end());
reverse(t.begin(),t.end());
int cnt=0;
bool flag=true;
for(int i=0;i<t.size();i++)
{
while(cnt<s.size() && ((cnt&1)!=(i&1) || t[i]!=s[cnt])) cnt++;
if(cnt==s.size())
{
flag=false;
break;
}
cnt++;
}
if(flag) puts("YES");
else puts("NO");
}
return 0;
}