华为机试题——HJ81 字符串字符匹配

描述

判断短字符串S中的所有字符是否在长字符串T中全部出现。

请注意本题有多组样例输入。

数据范围:1\le len(S),len(T)\le200\1≤len(S),len(T)≤200 

进阶:时间复杂度:O(n)\O(n) ,空间复杂度:O(n)\O(n) 

输入描述:

输入两个字符串。第一个为短字符串,第二个为长字符串。两个字符串均由小写字母组成。

输出描述:

如果短字符串的所有字符均在长字符串中出现过,则输出字符串"true"。否则输出字符串"false"。

示例1

输入:

bc
abc

复制输出:

true

复制说明:

其中abc含有bc,输出"true"
#include <iostream>
#include <string.h>
#include <set>

bool stringMatching(std::string shortStr, std::string longStr)
{
    std::set<char> shortSet;
    for(int i = 0; i < shortStr.length(); ++i)
    {
        shortSet.insert(shortStr[i]);
    }
    std::set<char> longSet;
    for(int i = 0; i < longStr.length(); ++i)
    {
        longSet.insert(longStr[i]);
    }

    shortStr.clear();
    std::set<char>::iterator itershort = shortSet.begin();
    while(itershort != shortSet.end())
    {
        shortStr += *itershort;
        itershort++;
    }


    longStr.clear();
    std::set<char>::iterator iterlong = longSet.begin();
    while(iterlong != longSet.end())
    {
        longStr += *iterlong;
        iterlong++;
    }

    for(int i = 0; i < shortStr.length(); ++i)
    {
        int count = 0;
        for(int j = 0; j < longStr.length(); ++j)
        {
            count++;
            char shortc = shortStr[i];
            char longc = longStr[j];
            if(shortStr[i] == longStr[j])
            {
                break;
            }
            if(count == longStr.length() - 1)
            {
                if(shortStr[i] != longStr[longStr.length()-1])
                {
                    return false;
                }
            }

        }
    }

    return true;
}

int main()
{
//     while(1)
//     {
        std::string shortStr;
        getline(std::cin, shortStr);

        std::string longStr;
        getline(std::cin, longStr);

        bool flag = stringMatching(shortStr, longStr);
        if(flag == 1)
        {
            std::cout << "true" << std::endl;
        }
        else
        {
            std::cout << "false" << std::endl;
        }

//     }


    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值