题目详情
这一题我用的遍历,直接求出。相对简单。
但是,使用find函数会更加简单
很棒的find详解
代码一、
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string a,b;
while(cin>>a>>b)
{
string c;
int n=a.length();
int t=b.length();
int k=0;
for(int i=0;i<n;i++)
{
for(int j=k;j<t;j++)
{
if(a[i]==b[j])
{
c+=b[j];
k=j+1;
break;
}
}
}
if(c==a) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
代码二、
#include<iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string s,t;
while(cin>>s>>t)
{
int x=0;
bool f=true;
for(int i=0;i<s.size();i++)
{
x=t.find(s[i],x)+1;//t从x开始,找s[i] ,找到后直接变成t中该位置的下一位
if(x==0)//没找到时find的返回值为-1
{
cout<<"No"<<endl;
f=false;
break;
}
}
if(f)
cout<<"Yes"<<endl;
}
return 0;
}