I am trying to solve this, but I am not sure what is wrong with my code. Can you tell me where is wrong?
If you just type one letter at a time, you will have to press the key for the length of A
.
To speed it up a bit, some string B is stored, so you can type the entire B
by pressing the key once.
It is not possible to erase already typed characters.
For example, when A = ”asakusa”
, B = ”sa”
, you can type A
in 5
times by using B
twice as shown in the following figure.
Given A
and B
, find the minimum number of times you have to press the key to type A in its entirety.
The first line is given the number T
of test cases.
For each test case, two strings A
and B
are given on the first line. The length of A
is 1
to 10,000
, and the length of B
is 1
to 100
.
Input:
1
apple ap
Output:
1 4
Code:
T = int(input())
for tc in range(1, T + 1):
A, B = input().split()
idx = 0
cnt = 0
N = len(A)
M = len(B)
for i in range(N):
if A[i] == B[idx]:
idx += 1
if idx == len(B):
cnt += 1
idx = 0
else:
continue
else:
idx = 0
print("#{} {}".format(tc, N - M * cnt + cnt))
I have found what you are missing. This case is failing
A,B = "aaaab", "aaab"
Your code outputs 5
, but it should be 2
. Think about this a little bit. If you still can't fix your code, I can give more info.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.