题目描述: 全量和已占用字符集 、字符串统计(分值100)
给定两个字符集合,一个是全量字符集,一个是已占用字符集,已占用字符集中的字符不能再使用。
要求输出剩余可用字符集。
输入描述
输入一个字符串 一定包含@,@前为全量字符集 @后的为已占用字符集
已占用字符集中的字符一定是全量字符集中的字符
字符集中的字符跟字符之间使用英文逗号隔开
每个字符都表示为字符+数字的形式用英文冒号分隔,比如a:1标识一个a字符
字符只考虑英文字母,区分大小写
数字只考虑正整型 不超过100
如果一个字符都没被占用 @标识仍存在,例如 a:3,b:5,c:2@
输出描述
输出可用字符集
不同的输出字符集之间用回车换行
注意 输出的字符顺序要跟输入的一致,如下面用例不能输出b:3,a:2,c:2
如果某个字符已全部占用 则不需要再输出
用例
输入
a:3,b:5,c:2@a:1,b:2
输出
a:2,b:3,c:2
说明
全量字符集为三个a,5个b,2个c
已占用字符集为1个a,2个b
由于已占用字符不能再使用
因此剩余可用字符为2个a,3个b,2个c
因此输出a:2,b:3,c:2
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100typedef struct {char ch[10];int count;
} charset;int main() {char line[MAX_LEN];fgets(line, MAX_LEN + 1, stdin);char *left = strtok(line, "@");char *right = strtok(NULL, "\n");charset fulllist[MAX_LEN];int fullcnt = 0;charset occlist[MAX_LEN];int occcnt = 0;// 解析全量char *token = strtok(left, ",");while (token != NULL) {sscanf(token, "%[^:]:%d", fulllist[fullcnt].ch,&fulllist[fullcnt].count);fullcnt++;token = strtok(NULL, ",");}// 解析已占用的if (right != NULL) {token = strtok(right, ",");while (token != NULL) {sscanf(token, "%[^:]:%d", occlist[occcnt].ch,&occlist[occcnt].count);occcnt++;token = strtok(NULL, ",");}}// 计算差值for (int i = 0; i < fullcnt; i++) {for (int j = 0; j < occcnt; j++) {if (strcmp(fulllist[i].ch, occlist[j].ch) == 0) {fulllist[i].count -= occlist[j].count;break;}}}for (int i = 0; i < fullcnt; i++) {if (fulllist[i].count > 0) {printf("%s:%d", fulllist[i].ch, fulllist[i].count);if (i < fullcnt - 1) {printf(",");}}}return 0;
}