CF:141A.Amusing Joke

        为了便于一次性比较,先将输入的前两个字符串连接起来,string类字符串用运算符“+”即可,就相当于C中的strcat( );连接函数。然后将连接起来后的字符串逐个与输入的第三个字符串比较,看是否能找到匹配,若找到匹配则另标记标量被赋值为1,并将第三个字符串中匹配的字符删除,以免下次重复比较,此过程中用到string类中的擦除函数:变量名.erase( ); 此函数括号里的参数可以是一个区间,也可以是指向某个字符的下标,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// string::erase
#include <iostream>
#include <string>

int main ()
{
  std::string str ("This is an example sentence.");
  std::cout << str << '\n';
                                           // "This is an example sentence."
  str.erase (10,8);                        //            ^^^^^^^^
  std::cout << str << '\n';
                                           // "This is an sentence."
  str.erase (str.begin()+9);               //           ^
  std::cout << str << '\n';
                                           // "This is a sentence."
  str.erase (str.begin()+5, str.end()-9);  //       ^^^^^
  std::cout << str << '\n';
                                           // "This sentence."
  return 0;
}


输出:

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;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值