E. 2-Letter Strings

这篇博客讨论了一种求解给定长度为2的字符串对中,一个位置字符相同另一个位置不同的数量的方法。作者首先介绍了题意,然后提出通过遍历并计算每个位置上相同字符的字符串数量来构建前缀和。注意到相等字符串的重复计数问题,作者利用map存储每个字符串出现的次数,避免重复贡献。最后,提供了AC代码实现。

https://codeforces.com/contest/1669/problem/E
我的做法是容斥一下?
虽然我的队友是转成图论用入度做的(雾
在这里插入图片描述
input

4
6
ab
cb
db
aa
cc
ef
7
aa
bb
cc
ac
ca
bb
aa
4
kk
kk
ab
ab
5
jf
jf
jk
jk
jk

output

5
6
0
6

在这里插入图片描述
题意
给定 n n n 个长度为 2 2 2 仅有小写字母 a − k a-k ak 的字符串,询问有多少对字符串满足其串上的两个位置,一个位置的字符相同,一个位置的字符不同
(可能看Note会更加理解)

思路
既然需要一个位置上的字符相同,那我从头向后遍历,查询该位置之后的字符串看对应位置上相等的字符串个数,比如:
观察样例 1 1 1,从位置 1 1 1 举例

  • 对于左边:
    发现只有一个 a a aa aa 的字符串与之配对,即贡献答案 1 1 1
    在这里插入图片描述
  • 对于右边
    可以发现在这之后有两个 b b b 出现,可以贡献答案 2 2 2
    在这里插入图片描述
    所以我们便可以对 左和右 两边的,每种字母的,做一个前缀和
    定义其数组为 : p r e [ i ] [ j ] [ k ] pre[i][j][k] pre
In the Morse code, letters and numbers are represented by sequences of dots (.) and dashes (-). For example, the letter A is represented as .-, B as -..., and so on. Write a function decodeDotDash(code: str, msg: str) -> str that decodes a message msg encoded with a dot/dash code provided in the code argument. The dot/dash code is given as a string shown in the example below (the pipe character | is used to separate codes for different symbols, and the colon : separates the letter/number from its dot/dash representation). The msg is a string containing the dot/dash-encoded message, where each symbol is separated by a space. The decoded message should be returned as a string. The example of usage is as follows: # The complete morseCode string is provided in the exam.ipynb file. morseCode = "A:.-|B:-...|C:-.-.|D:-..|E:.|F:..-.|G:--.|H:....|I:..|J:.---" #... decodeDotDash(code = morseCode, msg = "... --- ...") # should return "SOS" During grading your function will be tested with various code and msg strings, including not properly formatted ones. - To get 10 points the function should return correct results for at least some properly formatted code and msg strings. - To get 12 points the function should return correct results for all tested properly formatted code and msg strings. - To get 15 points, the function additionally should raise exceptions with **informative messages** for wrongly formatted msg or code (e.g. wrong characters used in dot/dash code). 请写出python代码
最新发布
06-14
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值