题解 | #字符串字符匹配#

字符串字符匹配

http://www.nowcoder.com/practice/22fdeb9610ef426f9505e3ab60164c93

#include<bits/stdc++.h>
using namespace std;
int main() {
    string sstr,lstr;    //sstr为短字符串,lstr为长字符串
    while(cin>>sstr>>lstr) {    //分别输入字符串sstr和lstr
        set<char> s1,s2,s;    //集合s1存放字符串sstr中的字符;集合s2存放字符串lstr中的字符,集合s存放集合s1和s2的交集
        for(int i=0;i<sstr.length();i++)    //将字符串sstr中的字符逐个添加到集合s1中
            s1.insert(sstr[i]);
        for(int i=0;i<lstr.length();i++)    //将字符串lstr中的字符逐个添加到集合s2中
            s2.insert(lstr[i]);
        set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s, s.begin()));    //将集合s1和s2的交集添加到集合s中
        if(s==s1)    //如果s1和s2的交集等于集合s1,字符串sstr中的所有字符,都在字符串lstr中出现了
            cout<<"true"<<endl;
        else    //否则,说明字符串sstr中并非所有字符都在字符串lstr中出现了
            cout<<"false"<<endl;
    }
    return 0;
}
全部评论

相关推荐

野猪不是猪🐗:😇:恭喜你以出色的表现成为xxx的一员 😨:您以进入本公司人才库 实际点开:您愿望单中的xxx正在特卖!
点赞 评论 收藏
分享
评论
5
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务