输入两个字符串s和t,判断是否可以从t中删除0个或多个字符(其他字符顺序不变)得到字符串s,例如abcde可以得到bce,但是得不到dc。
#include<iostream>
#include<string>
using namespace std;
int main()
{
bool ok = true;
string s,t;
cin >> s;
cin >> t;
int szs = int(s.length());
int szt = int(t.length());
for(int i = 0 ,j = 0 ; i < szs ; i++,j++)//使用两个变量i和j对两个字符串s和t同时进行遍历
{
while(j < szt && t[j] != s[i])
{
j++;
if(j == szt)//如果j越界,说明t[j++]中不存在等于s[i]的字符,查找失败
{
ok = false;
break;
}
}
//如果对于每个i配对成功,则说明问题有解,否则无法从t中删除字符得到s。
if(ok) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}