http://zju.acmclub.com/index.php?app=problem_title&id=1&problem_id=1055
需要注意的是,‘aaaa’的子串‘aa’在其中出现的次数为3,而不是2;如果用string.count()得到的结果是2。
故自己写一个函数,使子串B在A中比较时每次只向右移1格,而不是移动len(B)格
代码:
#b in a
def mycount(a,b):
if a=='' or b=='':
return 0
count = 0
j = 0
while True:
if b == a[j:(len(b)+j)]:
count += 1
j += 1
if j + len(b)>len(a):break
return count
while True:
A = raw_input()
B = raw_input()
lst = []
sumi = 0
for i in range(0,len(B)):
lst.append(B)
B = B[1:len(B)] + B[0]
lst2 = list(set(lst))
#print lst2
for i in lst2:
sumi += mycount(A,i)
print sumi