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