每日一题-A+B-

[每日一题] (A+B-)
给出两个字符串,s1和s2。s2中的+表示前面的字符重复2次,-代表前面的字符重复4次。问题是求s1中有多少个连续或不连续的s2。
例子: s1 = “aaabbbb” , s2 = “a+b-“
输出: 3

解题思路


使用动态规划。具体思路说起来有点绕…总之算是一个比较麻烦的动态规划题。

用n数组存储到当前位置为止有多少个n子串,用n_1数组存储到当前位置为止有多少个n-1子串
状态转移函数是n[i] = n[i-1] + n_1[i-1] + n_1[i-2] + … + n_1[2]或n_1[4],大概就是这个意思

代码没有经过很多测试,可能会有bug

Python:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#s1 = "aabbbbaacc"
#s2 = "a+b-c+"
#s1 = "aaabababbaabbccc"
#s2 = "a+b-c+"
#s1 = "aababaaabaaab"
#s1 = "aabaabaabcc"
#s2 = "a+b+c+"
#s1 = "waeginsapnaabangpisebbasepgnccccapisdnfngaabndlrjngeuiogbbegbuoecccc"
#s2 = "a+b+c-"
s1 = "wweqwqaabddakswbbcc"
s2 = "a+b+"
class Solution(object):
def aPlusbPlus(self, s1, s2):
pre = {} # 记录当前字符的前一字符
index = {} # 记录当前字符位置
count = {} # 记录当前字符找了几个
bound = {} # 记录当前字符要找几个
p = None
for i in xrange(len(s2)/2):
pre[s2[i*2]] = p
index[s2[i*2]] = 0
count[s2[i*2]] = 0
bound[s2[i*2]] = 2 if s2[i*2+1] == '+' else 4
p = s2[i*2]
n_1 = [[0, 0] for i in xrange(len(s1))]
n = [[0, 0] for i in xrange(len(s1))]
for i in xrange(len(s1)):
c = s1[i]
if c in index.keys():
if c == s2[0]: # 当前字符为s2首字符
count[c] += 1
sum = 1
for j in xrange(bound[c]):
sum *= count[c] - j
n[i] = [sum / bound[c], c]
n_1[i] = [0, c]
index[c] = i
else:
count[c] += 1
n_1[i] = [n[index[pre[c]]][0], c]
if count[c] < bound[c]:
n[i] = [0, c]
else :
n[i] = [n[index[c]][0], c]
count1, j = count[c] - 1, i-1
while count1 >= bound[c]-1 and j >= 0:
if n_1[j][1] == c:
n[i][0] += n_1[j][0]
count1 -= 1
j -= 1
index[c] = i
return n[index[s2[len(s2)-2]]][0]