华为笔试题

1、全量字符集与已占用字符集 

输入描述:
输入一个字符串,字符串中包含了全量字符集和已占用字符集,两个字符集用@相连。@前的字符集合为全量字符集,@后的字符集为已占用字符集合。已占用字符集中的字符一定是全量字符集中的字符。字符集中的字符跟字符之间使用英文逗号分隔。字符集中的字符表示为字符加数字,字符跟数字使用英文冒号分隔,比如a:1,表示1个a字符。字符只考虑英文字母,区分大小写,数字只考虑正整形,数量不超过100,如果一个字符都没被占用,@标识符仍在,例如a:3,b:5,c:2@

输出描述:
可用字符集。输出带回车换行。

示例1:
输入:a:3,b:5,c:2@a:1,b:2

输出:a:2,b:3,c:2

说明:全量字符集为3个a,5个b,2个c。已占用字符集为1个a,2个b。由于已占用字符集不能再使用,因此,剩余可用字符为2个a,3个b,2个c。因此输出a:2,b:3,c:2。注意,输出的字符顺序要跟输入一致。不能输出b:3,a:2,c:2。如果某个字符已全被占用,不需要输出。例如a:3,b:5,c:2@a:3,b:2,输出为b:3,c:2。

 用正则表达式分割。

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str;
    getline(cin, str);
    int i = 0, n = str.size();
    while(i < n && str[i] != '@') i++;
    string all = str.substr(0, i);
    i++;
    string used;
    if(i < n) used = str.substr(i);
    regex re(R"(([a-zA-Z]):(\d+))");
    vector<int> hash(256, 0);
    int cnt = 0;
    vector<char> input;
    for(sregex_token_iterator it(all.begin(), all.end(), re), end; it != end; it++)
    {
        string sub = *it;
        smatch m;
        if(regex_match(sub, m, re))
        {
            string character = m[1];
            int val = stoi(m[2]);
            hash[character[0]] = val;
            if(val > 0) cnt++;
            input.emplace_back(character[0]);
        }
    }
    for(sregex_token_iterator it(used.begin(), used.end(), re), end; it != end; it++)
    {
        string sub = *it;
        smatch m;
        if(regex_match(sub, m, re))
        {
            string ch = m[1];
            int val = stoi(m[2]);
            hash[ch[0]] -= val;
            if(hash[ch[0]] == 0) cnt--;
        }
    }
    for (int j = 0; j < input.size(); ++j)
    {
        if(hash[input[j]] > 0)
        {
            printf("%c:%d", input[j], hash[input[j]]);
            cnt--;
            if(cnt > 0) printf(";");
        }
    }
    return 0;
}

 

 2、逻辑计算

题目描述:

常用的逻辑计算有And(表示为&);Or(表示为|);Not(表示为!)。其中,他们的优先级关系是Not(!)>And

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值