全量和已占用字符集
题目描述
给定两个字符集合,一个为全量字符集,一个为已占用字符集。已占用的字符集中的字符不能再使用,要求输出剩余可用字符集。
输入描述
- 输入为一个字符串,一定包含@符号。@前的为全量字符集,@后的字为已占用字符集。
- 已占用字符集中的字符一定是全量字符集中的字符。字符集中的字符跟字符之间用英文逗号分隔。
- 每个字符都表示为字符加数字的形式,用英文冒号分割,比如a:1,表示1个a字符。
- 字符只考虑英文字母,区分大小写,数字只考虑正整形,数量不超过100。
- 如果一个字符都没被占用,@标识仍然存在,例如a:3,b:5,c:2@
输出描述
输出可用字符集,不同的输出字符集之间回车换行。
注意,输出的字符顺序要跟输入一致。不能输出b:3,a:2,c:2
如果某个字符已经全部被占用,不需要再输出。
示例1
输入
a:3,b:5,c:2@a:1,b:2
输出
a:2,b:3,c:2
代码:python
def OperationString(stringArr): # 返回字符串
i = 0
wordCount = {}
for s in stringArr:
putDir(s, i, wordCount)
i += 1
s = ''
count = len(wordCount.keys())
for item in wordCount.keys():
count -= 1
if wordCount[item] != 0:
if count > 0:
s += (item+":"+str(wordCount[item])+",")
else:
s += (item + ":" + str(wordCount[item]))
return s
def putDir(s, flag, wordCount):
chNumlst = s.split(",")
for item in chNumlst:
lst = item.split(":")
ch = lst[0]
num = eval(lst[1])
if flag != 1:
wordCount[ch] = num
else:
if wordCount.get(ch) != None:
temp = wordCount[ch]
wordCount[ch] = temp - num
else:
wordCount[ch] = num
if __name__ == '__main__':
s = input()
stringArr = s.strip().split("@")
s = OperationString(stringArr) # 把字符串分割成2个部分
print(s)
代码:java-v1
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main01 {
public static void solution01(){
// 获取输入
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] s_list = s.split("@");
// 边界条件判断
if (s_list[1].equals("")) {
System.out.println(s);
return;
}
ArrayList<String> in_key_order_list = new ArrayList<>(); // 存储输入的字符顺序
Map<String, Integer> d_map = new HashMap<>(); // 存储字符的个数
for (int i=0; i<s_list.length; i++) {
String[] curr_s_list = s_list[i].split(",");
if (i == 0) {
for (int j=0; j<curr_s_list.length; j++) {
String[] item_list = curr_s_list[j].split(":");
String key = item_list[0];
Integer value = Integer.parseInt(item_list[1]);
d_map.put(key, value);
in_key_order_list.add(key);
}
} else {
for (int j=0; j<curr_s_list.length; j++) {
String[] item_list = curr_s_list[j].split(":");
String key = item_list[0];
Integer value = Integer.parseInt(item_list[1]);
d_map.put(key, d_map.get(key) - value);
}
}
}
for (int i=0; i<in_key_order_list.size(); i++) {
String key = in_key_order_list.get(i);
int value = d_map.get(key);
if (value != 0) {
if (i != in_key_order_list.size() - 1) {
System.out.print(key + ":" + value + ",");
} else {
System.out.println(key + ":" + value);
}
}
}
}
public static void main(String[] args) {
solution01();
}
}
代码:java-v2
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
// 切分全量字符集和已占用字符集R
String[] parts = input.split("@");
if (parts.length == 1) {
System.out.println(parts[0]);
return;
}
String fullCharacterSetStr = parts[0];
String occupiedCharacterSetStr = parts[1];
// 解析全量字符集和已占用字符集
Map<Character, Integer> fullCharacterSet = parseCharacterSet(fullCharacterSetStr);
Map<Character, Integer> occupiedCharacterSet = parseCharacterSet(occupiedCharacterSetStr);
// 存储输入的字符顺序
ArrayList<Character> inKeyOrder = new ArrayList<>();
String[] charInfoArray = fullCharacterSetStr.split(",");
for (String charInfo : charInfoArray) {
String[] item = charInfo.split(":");
char charKey = item[0].charAt(0);
inKeyOrder.add(charKey);
}
// 计算可用字符集
Map<Character, Integer> availableCharacterSet = new HashMap<>(fullCharacterSet);
for (Character charKey : occupiedCharacterSet.keySet()) {
if (availableCharacterSet.containsKey(charKey)) {
int availableCount = availableCharacterSet.get(charKey) - occupiedCharacterSet.get(charKey);
if (availableCount > 0) {
availableCharacterSet.put(charKey, availableCount);
} else {
availableCharacterSet.remove(charKey);
}
}
}
// 输出可用字符集
StringBuilder printInfo = new StringBuilder();
for (Character key : inKeyOrder) {
if (availableCharacterSet.containsKey(key)) {
printInfo.append(key + ":" + availableCharacterSet.get(key) + ",");
}
}
String printInfoStr = printInfo.toString();
System.out.println(printInfoStr.substring(0, printInfoStr.length() - 1));
}
// 解析字符集字符串为 Map
private static Map<Character, Integer> parseCharacterSet(String charSetStr) {
Map<Character, Integer> charSet = new HashMap<>();
String[] charInfoArray = charSetStr.split(",");
for (String charInfo : charInfoArray) {
String[] parts = charInfo.split(":");
char charKey = parts[0].charAt(0);
int count = Integer.parseInt(parts[1]);
charSet.put(charKey, count);
}
return charSet;
}
}
代码:c++
#include <iostream>
#include <vector>
#include <map>
#include <sstream>
using namespace std;
void solution01() {
// 获取输入
string str;
getline(cin, str);
istringstream ss(str);
string token;
vector<string> arr_str;
while (getline(ss, token, '@')) {
arr_str.push_back(token);
}
// 边界条件判断
if (arr_str[1] == "") {
cout << str << endl;
return;
}
vector<string> in_key_order_list; // 存储输入的字符顺序
map<string, int> d_map; // 存储字符的个数
for (int i = 0; i < arr_str.size(); i++) {
istringstream ss2(arr_str[i]);
string token2;
vector<string> s2_arr;
while (getline(ss2, token2, ',')) {
s2_arr.push_back(token2);
}
if (i == 0) {
for (int j = 0; j < s2_arr.size(); j++) {
istringstream ss3(s2_arr[j]);
string token3;
vector<string> item_list;
while (getline(ss3, token3, ':')) {
item_list.push_back(token3);
}
string key = item_list[0];
int value = stoi(item_list[1]);
d_map[key] = value;
in_key_order_list.push_back(key);
}
} else {
for (int j = 0; j < s2_arr.size(); j++) {
istringstream ss3(s2_arr[j]);
string token3;
vector<string> item_list;
while (getline(ss3, token3, ':')) {
item_list.push_back(token3);
}
string key = item_list[0];
int value = stoi(item_list[1]);
d_map[key] = d_map[key] - value;
}
}
}
map<string, int> not_none_dic;
for (auto const& x : d_map) {
if (x.second != 0) {
not_none_dic[x.first] = x.second;
}
}
int d_len = not_none_dic.size();
int index = 0;
for (auto const& x : not_none_dic) {
if (index != d_len - 1) {
cout << x.first << ":" << x.second << ",";
} else {
cout << x.first << ":" << x.second;
}
index += 1;
}
}
int main() {
solution01();
return 0;
}
代码:js
function operationString(stringArr) {
let i = 0;
let wordCount = {};
for (let s of stringArr) {
putDir(s, i, wordCount);
i++;
}
let result = '';
let count = Object.keys(wordCount).length;
for (let item in wordCount) {
count--;
if (wordCount[item] !== 0) {
if (count > 0) {
result += item + ':' + wordCount[item] + ',';
} else {
result += item + ':' + wordCount[item];
}
}
}
return result;
}
function putDir(s, flag, wordCount) {
let chNumlst = s.split(',');
for (let item of chNumlst) {
let lst = item.split(':');
let ch = lst[0];
let num = eval(lst[1]);
if (flag !== 1) {
wordCount[ch] = num;
} else {
if (wordCount[ch] !== undefined) {
let temp = wordCount[ch];
wordCount[ch] = temp - num;
} else {
wordCount[ch] = num;
}
}
}
}
// Example usage
let s = "a:3,b:5,c:2@a:1,b:2";
let stringArr = s.trim().split("@");
let result = operationString(stringArr);
console.log(result);