OD 算法题 B卷【计算误码率】
文章目录
- 计算误码率
计算误码率
- 误码率 = 错误比特数 / 总比特数;以字符串表示总位数信息,错误字符数即为错误比特数;
- 字符串会被压缩,如2A3B4D5X 表示“AABBBDDDDXXXXX”;
- 第一次输入原始的压缩字符串,第二次输入出错的压缩字符串;
- 解压后两个字符串长度相等,计算误码率;
示例1
输入:
3A3B
2A4B
输出:
1/6
示例2
输入:
5Y5Z
5Y5Z
输出:
0/10
示例3
输入:
4Y5Z
9Y
输出:
5/9
示例4
输入:
40Y5Z
45Y
输出:
5/45
说明: 还原字符串的时候,注意方式,如45Y应该还原为45个Y+5个Y,逐字符计算整数值;
思路:
- 还原字符串,并逐位对比;
s1 = input()
s2 = input()
a_list = []
b_list = []total = 0 # 计算总字符数i = 0
while i < len(s1): # 每个字符为数字时,都向后拼接指定个数的字符num = 0while i < len(s1) and s1[i].isdigit():num *= 10num += int(s1[i])i += 1# 非数字的字符c = s1[i]a_list.append([c, num])total += numi += 1i = 0
while i < len(s2):num = 0while i < len(s2) and s2[i].isdigit():num *= 10num += int(s2[i])i += 1c = s2[i]b_list.append([c, num])i += 1index = 0
tmp1 = 0
tmp2 = 0
i = 0
j = 0
count = 0while i < len(a_list) and j < len(b_list):chars1 = a_list[i]chars2 = b_list[j]tmp1 += chars1[1]tmp2 += chars2[1]chars1[1] = 0chars2[1] = 0now1 = chars1[0]now2 = chars2[0]while index < tmp1 and index < tmp2:if now1 != now2:count += 1index += 1if index >= tmp1:i += 1if index >= tmp2:j += 1print(str(count) + "/" + str(total))