为了便于一次性比较,先将输入的前两个字符串连接起来,string类字符串用运算符“+”即可,就相当于C中的strcat( );连接函数。然后将连接起来后的字符串逐个与输入的第三个字符串比较,看是否能找到匹配,若找到匹配则另标记标量被赋值为1,并将第三个字符串中匹配的字符删除,以免下次重复比较,此过程中用到string类中的擦除函数:变量名.erase( ); 此函数括号里的参数可以是一个区间,也可以是指向某个字符的下标,如:
| |
输出:
This is an example sentence. This is an sentence. This is a sentence. This sentence. |
若未找到匹配的,则输出“NO”,退出。注意:就算完全匹配了,也要看第三个字符串是否有多余的字符,这个在题目下方的NOTE里有说明。
代码如下:
#include<iostream>
#include<string>
using namespace std;
int main(){
string ge,ho,pl;
int t;
while(cin>>ge>>ho>>pl){
string str=ge+ho;
string s1=pl;
for(int i=0;i<str.length();++i){
t=0;
for(int j=0;j<pl.length();++j){
if(str[i]==pl[j]){
pl.erase (pl.begin()+j);
t=1;
break;
}
}
if(t==0){
cout<<"NO"<<'\n';
break;
}
}
if(t){
if(str.length()==s1.length())
cout<<"YES"<<'\n';
else
cout<<"NO"<<'\n';
}
}
return 0;
}