message stringlengths 2 23.8k | message_type stringclasses 2
values | message_id int64 0 1 | conversation_id int64 97 109k | cluster float64 0 0 | __index_level_0__ int64 194 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him!
Input
The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.
Output
Output the only number — the amount of different substrings of t that start with sbegin and end with send.
Examples
Input
round
ro
ou
Output
1
Input
codeforces
code
forca
Output
0
Input
abababab
a
b
Output
4
Input
aba
ab
ba
Output
1
Note
In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.
In the fourth sample identificators intersect. | instruction | 0 | 68,988 | 0 | 137,976 |
Tags: brute force, data structures, hashing, strings
Correct Solution:
```
def calc_lcp(s, sa):
rank = [0 for _ in range(len(s))]
for i in range(len(s)):
rank[sa[i]] = i
lcp = [0 for _ in range(len(s) - 1)]
h = 0
for i in range(len(s)):
if rank[i] < len(s) - 1:
while max(i, sa[rank[i] + 1]) + h < len(s) and s[i + h] == s[sa[rank[i] + 1] + h]:
h += 1
lcp[rank[i]] = h
if h > 0:
h -= 1
return lcp, rank
def countinSort(array, key):
max_val = max(key)
cnt = [0 for _ in range(max_val + 1)]
for i in key:
cnt[i] += 1
for i in range(1, len(cnt)):
cnt[i] += cnt[i - 1]
resp = [0 for _ in array]
for i in range(len(array) - 1, -1, -1):
cnt[key[array[i]]] -= 1
resp[cnt[key[array[i]]]] = array[i]
return resp
def suffix_array(s):
s += '\0'
sa = [i for i in range(len(s))]
sa = countinSort(sa, [ord(c) for c in s])
rank = 0
ranks = [0 for _ in range(len(s))]
for i in range(1, len(s)):
if s[sa[i - 1]] != s[sa[i]]:
rank += 1
ranks[sa[i]] = rank
k = 1
while k < len(s):
sa = countinSort(sa, [ranks[i + k] if i + k < len(s) else 0 for i in range(len(s))])
sa = countinSort(sa, ranks)
rank = 0
rank_new = [0 for _ in range(len(s))]
for i in range(1, len(s)):
if ranks[sa[i - 1]] != ranks[sa[i]] or ranks[sa[i - 1] + k] != ranks[sa[i] + k]:
rank += 1
rank_new[sa[i]] = rank
ranks = rank_new
k *= 2
return sa[1:]
def kmp(s, p):
pi = [0 for _ in range(len(p))]
k = 0
for i in range(1, len(p)):
while k > 0 and p[k] != p[i]:
k = pi[k - 1]
if p[k] == p[i]:
k += 1
pi[i] = k
k = 0
resp = []
for i in range(len(s)):
while k > 0 and p[k] != s[i]:
k = pi[k - 1]
if p[k] == s[i]:
k += 1
if k == len(p):
resp.append(i - len(p) + 1)
k = pi[k - 1]
return resp
def lower_bound(list, value):
left = 0
right = len(list)
while left < right:
mid = int((left + right) / 2)
if list[mid] < value:
left = mid + 1
else:
right = mid
return left
s = input()
start = input()
end = input()
indStart = kmp(s, start)
indEnd = kmp(s, end)
if len(indStart) == 0 or len(indEnd) == 0:
print(0)
else:
sa = suffix_array(s)
lcp, rank = calc_lcp(s, sa)
ind = rank[indStart[0]]
for st in indStart:
ind = min(ind, rank[st])
resp = len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, len(start) - len(end)))
while ind < len(lcp) and lcp[ind] >= len(start):
ind += 1
resp += len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, max(lcp[ind - 1] + 1, len(start)) - len(end)))
print(resp)
``` | output | 1 | 68,988 | 0 | 137,977 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him!
Input
The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.
Output
Output the only number — the amount of different substrings of t that start with sbegin and end with send.
Examples
Input
round
ro
ou
Output
1
Input
codeforces
code
forca
Output
0
Input
abababab
a
b
Output
4
Input
aba
ab
ba
Output
1
Note
In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.
In the fourth sample identificators intersect. | instruction | 0 | 68,989 | 0 | 137,978 |
Tags: brute force, data structures, hashing, strings
Correct Solution:
```
def calc_lcp(s, sa):
n = len(s)
rank = [0 for _ in range(n)]
for i in range(n):
rank[sa[i]] = i
lcp = [0 for _ in range(n - 1)]
h = 0
for i in range(n):
if rank[i] < n - 1:
while max(i, sa[rank[i] + 1]) + h < n and s[i + h] == s[sa[rank[i] + 1] + h]:
h += 1
lcp[rank[i]] = h
if h > 0:
h -= 1
return lcp, rank
def suffix_array(s):
def countinSort(array, key):
max_val = max(key)
cnt = [0 for _ in range(max_val + 1)]
for i in key:
cnt[i] += 1
for i in range(1, len(cnt)):
cnt[i] += cnt[i - 1]
resp = [0 for _ in array]
for i in range(len(array) - 1, -1, -1):
cnt[key[array[i]]] -= 1
resp[cnt[key[array[i]]]] = array[i]
return resp
n = len(s)
sa = [i for i in range(n)]
ranks = [ord(c) for c in s]
k = 1
while k < n:
sa = countinSort(sa, [ranks[i + k] if i + k < n else -1 for i in range(n)])
sa = countinSort(sa, ranks)
rank_new = [0 for _ in range(n)]
for i in range(1, n):
if ranks[sa[i - 1]] == ranks[sa[i]] and sa[i] + k < n and sa[i - 1] + k < n and ranks[sa[i - 1] + k] == ranks[sa[i] + k]:
rank_new[sa[i]] = rank_new[sa[i - 1]]
else:
rank_new[sa[i]] = i
ranks = rank_new
k *= 2
return sa
def kmp(s, p):
m = len(p)
pi = [0 for _ in range(m)]
k = 0
for i in range(1, m):
while k > 0 and p[k] != p[i]:
k = pi[k - 1]
if p[k] == p[i]:
k += 1
pi[i] = k
k = 0
n = len(s)
resp = []
for i in range(n):
while k > 0 and p[k] != s[i]:
k = pi[k - 1]
if p[k] == s[i]:
k += 1
if k == m:
resp.append(i - m + 1)
k = pi[k - 1]
return resp
def lower_bound(list, value):
left = 0
right = len(list)
while left < right:
mid = int((left + right) / 2)
if list[mid] < value:
left = mid + 1
else:
right = mid
return left
s = input()
start = input()
end = input()
indStart = kmp(s, start)
indEnd = kmp(s, end)
if len(indStart) == 0 or len(indEnd) == 0:
print(0)
else:
sa = suffix_array(s)
lcp, rank = calc_lcp(s, sa)
ind = rank[indStart[0]]
for st in indStart:
ind = min(ind, rank[st])
resp = len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, len(start) - len(end)))
while ind < len(lcp) and lcp[ind] >= len(start):
ind += 1
resp += len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, max(lcp[ind - 1] + 1, len(start)) - len(end)))
print(resp)
``` | output | 1 | 68,989 | 0 | 137,979 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him!
Input
The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.
Output
Output the only number — the amount of different substrings of t that start with sbegin and end with send.
Examples
Input
round
ro
ou
Output
1
Input
codeforces
code
forca
Output
0
Input
abababab
a
b
Output
4
Input
aba
ab
ba
Output
1
Note
In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.
In the fourth sample identificators intersect.
Submitted Solution:
```
from functools import cmp_to_key
def calc_lcp(s, sa):
rank = [0 for _ in range(len(s))]
for i in range(len(s)):
rank[sa[i]] = i
lcp = [0 for _ in range(len(s) - 1)]
h = 0
for i in range(len(s)):
if rank[i] < len(s) - 1:
while max(i, sa[rank[i] + 1]) + h < len(s) and s[i + h] == s[sa[rank[i] + 1] + h]:
h += 1
lcp[rank[i]] = h
if h > 0:
h -= 1
return lcp, rank
def suffix_array(s):
sa = [i for i in range(len(s))]
rank = [ord(s[i]) for i in range(len(s))]
k = 1
while k < len(s):
key = [0 for _ in range(len(s))]
base = max(rank) + 2
for i in range(len(s)):
key[i] = rank[i] * base + (rank[i + k] + 1 if i + k < len(s) else 0)
sa.sort(key=(lambda i: key[i]))
rank[sa[0]] = 0
for i in range(1, len(s)):
rank[sa[i]] = rank[sa[i - 1]] if key[sa[i - 1]] == key[sa[i]] else i
k *= 2
# for i in sa:
# print(s[i:])
return sa
def kmp(s, p):
pi = [0 for _ in range(len(p))]
k = 0
for i in range(1, len(p)):
while k > 0 and p[k] != p[i]:
k = pi[k - 1]
if p[k] == p[i]:
k += 1
pi[i] = k
k = 0
resp = []
for i in range(len(s)):
while k > 0 and p[k] != s[i]:
k = pi[k - 1]
if p[k] == s[i]:
k += 1
if k == len(p):
resp.append(i - len(p) + 1)
k = pi[k - 1]
return resp
def lower_bound(list, value):
left = 0
right = len(list)
while left < right:
mid = int((left + right) / 2)
if list[mid] < value:
left = mid + 1
else:
right = mid
return left
s = input()
start = input()
end = input()
indStart = kmp(s, start)
indEnd = kmp(s, end)
if len(indStart) == 0 or len(indEnd) == 0:
print(0)
else:
sa = suffix_array(s)
lcp, rank = calc_lcp(s, sa)
ind = rank[indStart[0]]
for st in indStart:
ind = min(ind, rank[st])
resp = len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, len(start) - len(end)))
while ind < len(lcp) and lcp[ind] >= len(start):
ind += 1
resp += len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, max(lcp[ind - 1] + 1, len(start)) - len(end)))
print(resp)
``` | instruction | 0 | 68,990 | 0 | 137,980 |
Yes | output | 1 | 68,990 | 0 | 137,981 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him!
Input
The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.
Output
Output the only number — the amount of different substrings of t that start with sbegin and end with send.
Examples
Input
round
ro
ou
Output
1
Input
codeforces
code
forca
Output
0
Input
abababab
a
b
Output
4
Input
aba
ab
ba
Output
1
Note
In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.
In the fourth sample identificators intersect.
Submitted Solution:
```
def calc_lcp(s, sa):
rank = [0 for _ in range(len(s))]
for i in range(len(s)):
rank[sa[i]] = i
lcp = [0 for _ in range(len(s) - 1)]
h = 0
for i in range(len(s)):
if rank[i] < len(s) - 1:
while max(i, sa[rank[i] + 1]) + h < len(s) and s[i + h] == s[sa[rank[i] + 1] + h]:
h += 1
lcp[rank[i]] = h
if h > 0:
h -= 1
return lcp, rank
def countinSort(array, key):
max_val = max(key)
cnt = [0 for _ in range(max_val + 1)]
for i in key:
cnt[i] += 1
for i in range(1, len(cnt)):
cnt[i] += cnt[i - 1]
resp = [0 for _ in array]
for i in range(len(array) - 1, -1, -1):
cnt[key[array[i]]] -= 1
resp[cnt[key[array[i]]]] = array[i]
return resp
def suffix_array(s):
s += '\0'
sa = [i for i in range(len(s))]
ranks = [ord(c) for c in s]
k = 1
while k < len(s):
sa = countinSort(sa, [ranks[i + k] if i + k < len(s) else 0 for i in range(len(s))])
sa = countinSort(sa, ranks)
rank_new = [0 for _ in range(len(s))]
for i in range(1, len(s)):
if ranks[sa[i - 1]] == ranks[sa[i]] and ranks[sa[i - 1] + k] == ranks[sa[i] + k]:
rank_new[sa[i]] = rank_new[sa[i - 1]]
else:
rank_new[sa[i]] = i
ranks = rank_new
k *= 2
return sa[1:]
def kmp(s, p):
pi = [0 for _ in range(len(p))]
k = 0
for i in range(1, len(p)):
while k > 0 and p[k] != p[i]:
k = pi[k - 1]
if p[k] == p[i]:
k += 1
pi[i] = k
k = 0
resp = []
for i in range(len(s)):
while k > 0 and p[k] != s[i]:
k = pi[k - 1]
if p[k] == s[i]:
k += 1
if k == len(p):
resp.append(i - len(p) + 1)
k = pi[k - 1]
return resp
def lower_bound(list, value):
left = 0
right = len(list)
while left < right:
mid = int((left + right) / 2)
if list[mid] < value:
left = mid + 1
else:
right = mid
return left
s = input()
start = input()
end = input()
indStart = kmp(s, start)
indEnd = kmp(s, end)
if len(indStart) == 0 or len(indEnd) == 0:
print(0)
else:
sa = suffix_array(s)
lcp, rank = calc_lcp(s, sa)
ind = rank[indStart[0]]
for st in indStart:
ind = min(ind, rank[st])
resp = len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, len(start) - len(end)))
while ind < len(lcp) and lcp[ind] >= len(start):
ind += 1
resp += len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, max(lcp[ind - 1] + 1, len(start)) - len(end)))
print(resp)
``` | instruction | 0 | 68,991 | 0 | 137,982 |
Yes | output | 1 | 68,991 | 0 | 137,983 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him!
Input
The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.
Output
Output the only number — the amount of different substrings of t that start with sbegin and end with send.
Examples
Input
round
ro
ou
Output
1
Input
codeforces
code
forca
Output
0
Input
abababab
a
b
Output
4
Input
aba
ab
ba
Output
1
Note
In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.
In the fourth sample identificators intersect.
Submitted Solution:
```
def calc_lcp(s, sa):
n = len(s)
rank = [0 for _ in range(n)]
for i in range(n):
rank[sa[i]] = i
lcp = [0 for _ in range(n - 1)]
h = 0
for i in range(n):
if rank[i] < n - 1:
while s[i + h] == s[sa[rank[i] + 1] + h]:
h += 1
lcp[rank[i]] = h
if h > 0:
h -= 1
return lcp, rank
def index_sort(r):
n = len(r)
mask = (1 << 32) - 1
ls = [(r[i] << 32) | i for i in range(n)]
ls.sort()
res = [i & mask for i in ls]
return res
def suffix_array(s):
n = len(s) - 1
rank = [ord(c) for c in s]
sa = index_sort(rank)
a = [0 for _ in range(n + 1)]
b = [0 for _ in range(n + 1)]
h = 0
while True:
for i in range(n):
x, y = sa[i + 1], sa[i]
b[i + 1] = b[i]
if rank[x] > rank[y] or rank[x + h] > rank[y + h]:
b[i + 1] += 1
for i in range(n + 1):
rank[sa[i]] = b[i]
if b[n] == n:
break
h = max(1, h << 1)
for k in range(h, -1, -h):
b = [0 for _ in range(n + 1)]
b[0] = k
for i in range(k, n + 1):
b[rank[i]] += 1
for i in range(n):
b[i + 1] += b[i]
for i in range(n, -1, -1):
r = 0 if sa[i] + k > n else rank[sa[i] + k]
b[r] -= 1
a[b[r]] = sa[i]
sa, a = a, sa
return sa
def kmp(s, p):
m = len(p)
pi = [0 for _ in range(m)]
k = 0
for i in range(1, m):
while k > 0 and p[k] != p[i]:
k = pi[k - 1]
if p[k] == p[i]:
k += 1
pi[i] = k
k = 0
n = len(s)
resp = []
for i in range(n):
while k > 0 and p[k] != s[i]:
k = pi[k - 1]
if p[k] == s[i]:
k += 1
if k == m:
resp.append(i - m + 1)
k = pi[k - 1]
return resp
def lower_bound(list, value):
left = 0
right = len(list)
while left < right:
mid = int((left + right) / 2)
if list[mid] < value:
left = mid + 1
else:
right = mid
return left
s = input()
start = input()
end = input()
indStart = kmp(s, start)
indEnd = kmp(s, end)
if len(indStart) == 0 or len(indEnd) == 0:
print(0)
else:
s += chr(0)
sa = suffix_array(s)
lcp, rank = calc_lcp(s, sa)
ind = rank[indStart[0]]
for st in indStart:
ind = min(ind, rank[st])
resp = len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, len(start) - len(end)))
while ind < len(lcp) and lcp[ind] >= len(start):
ind += 1
resp += len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, max(lcp[ind - 1] + 1, len(start)) - len(end)))
print(resp)
``` | instruction | 0 | 68,992 | 0 | 137,984 |
Yes | output | 1 | 68,992 | 0 | 137,985 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him!
Input
The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.
Output
Output the only number — the amount of different substrings of t that start with sbegin and end with send.
Examples
Input
round
ro
ou
Output
1
Input
codeforces
code
forca
Output
0
Input
abababab
a
b
Output
4
Input
aba
ab
ba
Output
1
Note
In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.
In the fourth sample identificators intersect.
Submitted Solution:
```
def calc_lcp(s, sa):
rank = [0 for _ in range(len(s))]
for i in range(len(s)):
rank[sa[i]] = i
lcp = [0 for _ in range(len(s) - 1)]
h = 0
for i in range(len(s)):
if rank[i] < len(s) - 1:
while max(i, sa[rank[i] + 1]) + h < len(s) and s[i + h] == s[sa[rank[i] + 1] + h]:
h += 1
lcp[rank[i]] = h
if h > 0:
h -= 1
return lcp, rank
def suffix_array(s):
s += chr(0)
n = len(s)
na = max(n, 256)
sa = [0 for _ in range(n)]
top = [0 for _ in range(na)]
rank = [0 for _ in range(n)]
sa_new = [0 for _ in range(n)]
rank_new = [0 for _ in range(n)]
for i in range(n):
rank[i] = ord(s[i])
top[rank[i]] += 1
for i in range(1, na):
top[i] += top[i - 1]
for i in range(n):
top[rank[i]] -= 1
sa[top[rank[i]]] = i
k = 1
while k < n:
for i in range(n):
j = sa[i] - k
if j < 0:
j += n
sa_new[top[rank[j]]] = j
top[rank[j]] += 1
rank_new[sa_new[0]] = 0
top[0] = 0
cnt = 0
for i in range(1, n):
if rank[sa_new[i]] != rank[sa_new[i - 1]] or rank[sa_new[i] + k] != rank[sa_new[i - 1] + k]:
cnt += 1
top[cnt] = i
rank_new[sa_new[i]] = cnt
sa, sa_new = sa_new, sa
rank, rank_new = rank_new, rank
if cnt == n - 1:
break
k *= 2
return sa[1:]
def kmp(s, p):
pi = [0 for _ in range(len(p))]
k = 0
for i in range(1, len(p)):
while k > 0 and p[k] != p[i]:
k = pi[k - 1]
if p[k] == p[i]:
k += 1
pi[i] = k
k = 0
resp = []
for i in range(len(s)):
while k > 0 and p[k] != s[i]:
k = pi[k - 1]
if p[k] == s[i]:
k += 1
if k == len(p):
resp.append(i - len(p) + 1)
k = pi[k - 1]
return resp
def lower_bound(list, value):
left = 0
right = len(list)
while left < right:
mid = int((left + right) / 2)
if list[mid] < value:
left = mid + 1
else:
right = mid
return left
s = input()
start = input()
end = input()
indStart = kmp(s, start)
indEnd = kmp(s, end)
if len(indStart) == 0 or len(indEnd) == 0:
print(0)
else:
sa = suffix_array(s)
lcp, rank = calc_lcp(s, sa)
ind = rank[indStart[0]]
for st in indStart:
ind = min(ind, rank[st])
resp = len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, len(start) - len(end)))
while ind < len(lcp) and lcp[ind] >= len(start):
ind += 1
resp += len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, max(lcp[ind - 1] + 1, len(start)) - len(end)))
print(resp)
``` | instruction | 0 | 68,993 | 0 | 137,986 |
Yes | output | 1 | 68,993 | 0 | 137,987 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him!
Input
The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.
Output
Output the only number — the amount of different substrings of t that start with sbegin and end with send.
Examples
Input
round
ro
ou
Output
1
Input
codeforces
code
forca
Output
0
Input
abababab
a
b
Output
4
Input
aba
ab
ba
Output
1
Note
In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.
In the fourth sample identificators intersect.
Submitted Solution:
```
MAX = 2005
K = [26]
PW = [0]*MAX
H = [[0]*MAX for i in range(4)]
def init():
PW[0] = 1
for i in range(1, MAX):
PW[i] = PW[i - 1] * K[0]
def v(c):
return ord(c) - 98
def build(s, h):
n = len(s)
H[h][0] = 0
for i in range(1, n+1):
H[h][i] = H[h][i - 1] * K[0] + v(s[i - 1])
def calc(a, b, h):
return H[h][b] - H[h][a] * PW[b - a]
init()
ss = str(input())
a = str(input())
b = str(input())
build(a, 0)
build(b, 1)
ans = [0]
for i in range(len(ss) - len(a) + 1):
build(ss[i:i+len(a)], 2)
if calc(0, len(a), 0) == calc(0, len(a), 2):
cont = 0
for j in range(i, len(ss) - len(b) + 1):
build(ss[j:j+len(b)], 3)
if calc(0, len(b), 1) == calc(0, len(b), 3):
cont += 1
ans.append(cont)
print(max(ans))
``` | instruction | 0 | 68,994 | 0 | 137,988 |
No | output | 1 | 68,994 | 0 | 137,989 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him!
Input
The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.
Output
Output the only number — the amount of different substrings of t that start with sbegin and end with send.
Examples
Input
round
ro
ou
Output
1
Input
codeforces
code
forca
Output
0
Input
abababab
a
b
Output
4
Input
aba
ab
ba
Output
1
Note
In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.
In the fourth sample identificators intersect.
Submitted Solution:
```
#learn python by pratice
import re
s,b,e=input(),input(),input()
ib=[m.start() for m in re.finditer("(?="+b+")",s)]
ie=[m.start() for m in re.finditer("(?="+e+")",s)]
print(len(set(s[x:y-x] for x in ib for y in ie if x<y)))
``` | instruction | 0 | 68,995 | 0 | 137,990 |
No | output | 1 | 68,995 | 0 | 137,991 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him!
Input
The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.
Output
Output the only number — the amount of different substrings of t that start with sbegin and end with send.
Examples
Input
round
ro
ou
Output
1
Input
codeforces
code
forca
Output
0
Input
abababab
a
b
Output
4
Input
aba
ab
ba
Output
1
Note
In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.
In the fourth sample identificators intersect.
Submitted Solution:
```
MAX = 2005
K = [26]
PW = [0]*MAX
H = [[0]*MAX for i in range(4)]
def init():
PW[0] = 1
for i in range(1, MAX):
PW[i] = PW[i - 1] * K[0]
def v(c): return ord(c) - 98
def build(s, h):
n = len(s)
H[h][0] = 0
for i in range(1, n+1):
H[h][i] = H[h][i - 1] * K[0] + v(s[i - 1])
def calc(a, b, h):
return H[h][b] - H[h][a] * PW[b - a]
init()
ss = str(input())
a = str(input())
b = str(input())
if len(a) <= len(ss) and len(b) <= len(ss):
build(a, 0)
build(b, 1)
ans = 0
for i in range(len(ss) - len(a) + 1):
build(ss[i:i+len(a)], 2)
if calc(0, len(a), 0) == calc(0, len(a), 2):
cont = 0
for j in range(i + 1, len(ss) - len(b) + 1):
build(ss[j:j+len(b)], 3)
if calc(0, len(b), 1) == calc(0, len(b), 3):
cont += 1
ans = max(ans, cont)
print(ans)
else:
print(0)
``` | instruction | 0 | 68,996 | 0 | 137,992 |
No | output | 1 | 68,996 | 0 | 137,993 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him!
Input
The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.
Output
Output the only number — the amount of different substrings of t that start with sbegin and end with send.
Examples
Input
round
ro
ou
Output
1
Input
codeforces
code
forca
Output
0
Input
abababab
a
b
Output
4
Input
aba
ab
ba
Output
1
Note
In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.
In the fourth sample identificators intersect.
Submitted Solution:
```
def calc_lcp(s, sa):
rank = [0 for _ in range(len(s))]
for i in range(len(s)):
rank[sa[i]] = i
lcp = [0 for _ in range(len(s) - 1)]
h = 0
for i in range(len(s)):
if rank[i] < len(s) - 1:
while max(i, sa[rank[i] + 1]) + h < len(s) and s[i + h] == s[sa[rank[i] + 1] + h]:
h += 1
lcp[rank[i]] = h
if h > 0:
h -= 1
return lcp, rank
def suffix_array(s):
def countinSort(array, key):
max_val = max(key)
cnt = [0 for _ in range(max_val + 1)]
for i in key:
cnt[i] += 1
for i in range(1, len(cnt)):
cnt[i] += cnt[i - 1]
resp = [0 for _ in array]
for i in range(len(array) - 1, -1, -1):
cnt[key[array[i]]] -= 1
resp[cnt[key[array[i]]]] = array[i]
return resp
sa = [i for i in range(len(s))]
ranks = [ord(c) for c in s]
k = 1
while k < len(s):
sa = countinSort(sa, [ranks[i + k] if i + k < len(s) else -1 for i in range(len(s))])
sa = countinSort(sa, ranks)
rank_new = [0 for _ in range(len(s))]
for i in range(1, len(s)):
if ranks[sa[i - 1]] == ranks[sa[i]] and sa[i] + k < len(s) and sa[i - 1] + k < len(s) and ranks[sa[i - 1] + k] == ranks[sa[i] + k]:
rank_new[sa[i]] = rank_new[sa[i - 1]]
else:
rank_new[sa[i]] = i
ranks = rank_new
k *= 2
for i in sa:
print(s[i:])
return sa
def kmp(s, p):
pi = [0 for _ in range(len(p))]
k = 0
for i in range(1, len(p)):
while k > 0 and p[k] != p[i]:
k = pi[k - 1]
if p[k] == p[i]:
k += 1
pi[i] = k
k = 0
resp = []
for i in range(len(s)):
while k > 0 and p[k] != s[i]:
k = pi[k - 1]
if p[k] == s[i]:
k += 1
if k == len(p):
resp.append(i - len(p) + 1)
k = pi[k - 1]
return resp
def lower_bound(list, value):
left = 0
right = len(list)
while left < right:
mid = int((left + right) / 2)
if list[mid] < value:
left = mid + 1
else:
right = mid
return left
s = input()
start = input()
end = input()
indStart = kmp(s, start)
indEnd = kmp(s, end)
if len(indStart) == 0 or len(indEnd) == 0:
print(0)
else:
sa = suffix_array(s)
lcp, rank = calc_lcp(s, sa)
ind = rank[indStart[0]]
for st in indStart:
ind = min(ind, rank[st])
resp = len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, len(start) - len(end)))
while ind < len(lcp) and lcp[ind] >= len(start):
ind += 1
resp += len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, max(lcp[ind - 1] + 1, len(start)) - len(end)))
print(resp)
``` | instruction | 0 | 68,997 | 0 | 137,994 |
No | output | 1 | 68,997 | 0 | 137,995 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. | instruction | 0 | 69,188 | 0 | 138,376 |
Tags: brute force
Correct Solution:
```
aa = input()
bb = input()
if len(aa) >= len(bb):
p = aa
q = bb
else:
p = aa
q = bb
maxc = 0
for i in range(1,len(p)+len(q)):
k1 = max(0,i-len(q))
k2 = min(i,len(p))
a = p[k1:k2]
if i < len(q):
b = q[-i:]
elif i > len(p):
b = q[:-(i-len(p))]
else:
b = q
c = 0
for j in range(len(a)):
if a[j] == b[j]:
c += 1
if c > maxc:
maxc = c
print(len(bb)-maxc)
``` | output | 1 | 69,188 | 0 | 138,377 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. | instruction | 0 | 69,189 | 0 | 138,378 |
Tags: brute force
Correct Solution:
```
s = input()
u = input()
t = [0] * len(s)
d = {chr(i) : [] for i in range(ord('a'), ord('z')+1)}
for i, j in enumerate(s) :
d[j].append(i)
for i in u:
for j in d[i]:
t[j] += 1
t = [0] + t
print(len(u) - max(t))
``` | output | 1 | 69,189 | 0 | 138,379 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. | instruction | 0 | 69,190 | 0 | 138,380 |
Tags: brute force
Correct Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz'
M=10**9+7
EPS=1e-6
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
def cost(x):
ans=0
for i in range(len(t)):
ans+=s[i+x]!=t[i]
return ans
s=input()
t=input()
n=len(s)
ans=inf
s='/'*(len(t))+s+'/'*(len(t))
for i in range(n+2*len(t)-len(t)):
ans=min(ans,cost(i))
print(ans)
``` | output | 1 | 69,190 | 0 | 138,381 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. | instruction | 0 | 69,191 | 0 | 138,382 |
Tags: brute force
Correct Solution:
```
def check(ss):
tmp=0
for i in range(len(u)):
tmp+=ss[i]!=u[i]
return tmp
s = input()
u = input()
s = '#'*2000 + s + '#'*2000
ans=1000000000000
for i in range(len(s)-len(u)):
sub = s[i:i+len(u)]
ans = min(ans , check(sub))
print(ans)
``` | output | 1 | 69,191 | 0 | 138,383 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. | instruction | 0 | 69,192 | 0 | 138,384 |
Tags: brute force
Correct Solution:
```
s=input()
u=input()
t=[0]*len(s)
d={i :[] for i in "abcdefghijklmnopqrstuvwxyz" }
for i,j in enumerate(s) :
d[j].append(i)
for i in u:
for j in d[i]:
t[j]=t[j]+1
t=[0]+t
#print(t)
print(len(u)-max(t))
``` | output | 1 | 69,192 | 0 | 138,385 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. | instruction | 0 | 69,193 | 0 | 138,386 |
Tags: brute force
Correct Solution:
```
from sys import stdin
first = stdin.readline().strip()
second = stdin.readline().strip()
minChange = 2001
first = '@' * (len(second) - 1) + first + '@' * (len(second) - 1)
diff = len(first) - len(second)
for i in range(0, diff + 1):
line = '@' * i + second + '@' * (diff - i)
result = 0
for i in range(i, i + len(second)):
if first[i] != line[i]:
result += 1
if minChange > result:
minChange = result
print(minChange)
``` | output | 1 | 69,193 | 0 | 138,387 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. | instruction | 0 | 69,194 | 0 | 138,388 |
Tags: brute force
Correct Solution:
```
x,y=input(),input()
res=[0]*len(x)
dicc ={i:[] for i in "abcdefghijklmnopqrstuvwxyz"}
for i,j in enumerate(x):
dicc[j].append(i)
for i in y:
for j in dicc[i]:
res[j]+=1
res=[0]+res
print(len(y)-max(res))
``` | output | 1 | 69,194 | 0 | 138,389 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. | instruction | 0 | 69,195 | 0 | 138,390 |
Tags: brute force
Correct Solution:
```
from sys import stdin, stdout
first, second = stdin.readline().strip(), stdin.readline().strip()
n, m = len(first), len(second)
first = '#' * m + first + '#' * m
ans = float('inf')
for i in range(n + m):
cnt = 0
for j in range(m):
if first[i + j] != second[j]:
cnt += 1
ans = min(cnt, ans)
stdout.write(str(ans))
``` | output | 1 | 69,195 | 0 | 138,391 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message.
Submitted Solution:
```
def get_similarity(str1, str2):
str_len = len(str1)
str_len = str_len if str_len < len(str2) else len(str2)
similarity = 0
for i in range(str_len):
if str1[i] == str2[i]:
similarity += 1
return similarity
s = input()
u = input()
cnt = 0
s_len = len(s)
u_len = len(u)
similarity_max = 0
for i in range(u_len):
similarity_this = get_similarity(u[i:i+u_len], s)
if similarity_max < similarity_this:
similarity_max = similarity_this
for i in range(s_len):
similarity_this = get_similarity(s[i:i+u_len], u)
if similarity_max < similarity_this:
similarity_max = similarity_this
print(u_len - similarity_max)
``` | instruction | 0 | 69,196 | 0 | 138,392 |
Yes | output | 1 | 69,196 | 0 | 138,393 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message.
Submitted Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------game starts now-----------------------------------------------------
s=input()
t=input()
r=0
if len(s)<len(t):
(s,t)=(t,s)
r+=len(s)-len(t)
mn=len(t)
s="A"*(mn-1)+s+"A"*(mn+1)
for i in range (len(s)-len(t)+1):
ch=0
for j in range (len(t)):
if s[i+j]!=t[j]:
ch+=1
#print(s[i],t[j])
mn=min(mn,ch)
#print(i,ch)
r+=mn
print(r)
``` | instruction | 0 | 69,197 | 0 | 138,394 |
Yes | output | 1 | 69,197 | 0 | 138,395 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message.
Submitted Solution:
```
x,y=input(),input()
t=[0]*len(x)
p={i:[] for i in "abcdefghijklmnopqrstuvwxyz"}
for i,j in enumerate(x):
p[j].append(i)
#print(p)
for i in y:
for j in p[i]:
t[j]+=1
t=[0]+t
print(len(y)-max(t))
``` | instruction | 0 | 69,198 | 0 | 138,396 |
Yes | output | 1 | 69,198 | 0 | 138,397 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message.
Submitted Solution:
```
s, u = input(), input()
t = [0] * len(s)
p = {i: [] for i in 'abcdefghijklmnopqrstuvwxyz'}
for i, j in enumerate(s): p[j].append(i)
for j in u:
for i in p[j]: t[i] += 1
t = [0] + t
print(len(u) - max(t))
``` | instruction | 0 | 69,199 | 0 | 138,398 |
Yes | output | 1 | 69,199 | 0 | 138,399 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message.
Submitted Solution:
```
from sys import stdin, stdout
first, second = stdin.readline().strip(), stdin.readline().strip()
n, m = len(first), len(second)
first += '#' * m
ans = float('inf')
for i in range(n):
cnt = 0
for j in range(m):
if first[i + j] != second[j]:
cnt += 1
ans = min(cnt, ans)
stdout.write(str(ans))
``` | instruction | 0 | 69,200 | 0 | 138,400 |
No | output | 1 | 69,200 | 0 | 138,401 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message.
Submitted Solution:
```
def get_similarity(str1, str2):
str_len = len(str1)
str_len = str_len if str_len < len(str2) else len(str2)
similarity = 0
for i in range(str_len):
if str1[i] == str2[i]:
similarity += 1
return similarity
s = input()
u = input()
cnt = 0
s_len = len(s)
u_len = len(u)
similar_str = ""
similarity_max = 0
for i in range(s_len):
similarity_this = get_similarity(s[i:i+u_len], u)
if similarity_max < similarity_this:
similarity_max = similarity_this
similar_str = s[i:i+u_len]
print(u_len - similarity_max)
``` | instruction | 0 | 69,201 | 0 | 138,402 |
No | output | 1 | 69,201 | 0 | 138,403 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message.
Submitted Solution:
```
s= input()
s1 = input()
count =1
for i in range(len(s1)):
if s1[:count] in s:
count+=1
if count==1:
for i in set(s1):
if i in s:
count+=1
break
print(len(s1)-count+1)
``` | instruction | 0 | 69,202 | 0 | 138,404 |
No | output | 1 | 69,202 | 0 | 138,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s.
String p is called a substring of string s if you can read it starting from some position in the string s. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
* Insert one letter to any end of the string.
* Delete one letter from any end of the string.
* Change one letter into any other one.
Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u.
Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.
Input
The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Output
Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
Examples
Input
aaaaa
aaa
Output
0
Input
abcabc
bcd
Output
1
Input
abcdef
klmnopq
Output
7
Note
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message.
Submitted Solution:
```
from sys import stdin
first = stdin.readline().strip()
second = stdin.readline().strip()
subset = []
for i in range(0, len(first)):
for j in range(1, len(first) + 1):
if first[i:j] == '':
continue
if first[i:j] not in subset:
subset.append(first[i:j])
minChange = -1
if second in subset:
minChange = 0
else:
for i in range(0, len(second)):
for j in range(1, len(second) + 1):
if second[i:j] == '':
continue
if second[i:j] in subset:
if minChange == -1:
minChange = len(second) - len(second[i:j])
continue
if minChange > len(second) - len(second[i:j]):
minChange = len(second) - len(second[i:j])
print(minChange)
``` | instruction | 0 | 69,203 | 0 | 138,406 |
No | output | 1 | 69,203 | 0 | 138,407 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal". | instruction | 0 | 69,265 | 0 | 138,530 |
Tags: implementation, strings, two pointers
Correct Solution:
```
s = input()
n = len(s)
ans = 0
m = 0
for i in range(n - 5, -1, -1):
if s[i:i + 5] == "metal":
m += 1
elif s[i:i + 5] == "heavy":
ans += m
print(ans)
``` | output | 1 | 69,265 | 0 | 138,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal". | instruction | 0 | 69,266 | 0 | 138,532 |
Tags: implementation, strings, two pointers
Correct Solution:
```
str = input()
h=0
m=0
for i in range(len(str)-4):
if str[i:i+5] =="heavy":
h+=1
if str[i:i+5] == "metal":
m+=h
print(m)
``` | output | 1 | 69,266 | 0 | 138,533 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal". | instruction | 0 | 69,267 | 0 | 138,534 |
Tags: implementation, strings, two pointers
Correct Solution:
```
a=input()
st=''
if len(a)<5:
print(0)
else:
for i in range(4,len(a)):
if a[i-4]=='h' and a[i-3]=='e' and a[i-2]=='a' and a[i-1]=='v' and a[i]=='y':
st+='h'
elif a[i-4]=='m' and a[i-3]=='e' and a[i-2]=='t' and a[i-1]=='a' and a[i]=='l':
st+='m'
totm=st.count('m')
tot=0
for i in st:
if i=='h':
tot+=totm
else:
totm-=1
print(tot)
``` | output | 1 | 69,267 | 0 | 138,535 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal". | instruction | 0 | 69,268 | 0 | 138,536 |
Tags: implementation, strings, two pointers
Correct Solution:
```
from sys import stdin,stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int,stdin.readline().split()))
for _ in range(1):#nmbr():
s=input()
n=len(s)
cnt=ans=0
for i in range(n):
if s[i-4:i+1]=='heavy':cnt+=1
if s[i-4:i+1]=='metal':ans+=cnt
print(ans)
``` | output | 1 | 69,268 | 0 | 138,537 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal". | instruction | 0 | 69,269 | 0 | 138,538 |
Tags: implementation, strings, two pointers
Correct Solution:
```
from functools import reduce
from operator import *
from math import *
from sys import *
from string import *
from collections import *
setrecursionlimit(10**7)
dX= [-1, 1, 0, 0,-1, 1,-1, 1]
dY= [ 0, 0,-1, 1, 1,-1,-1, 1]
RI=lambda: list(map(int,input().split()))
RS=lambda: input().rstrip().split()
#################################################
s=RS()[0]
h,m=[],[]
i=s.find("heavy")
while i!=-1:
h.append(i)
i=s.find("heavy",i+1)
j=s.find("metal")
while j!=-1:
m.append(j)
j=s.find("metal",j+1)
i,j=0,0
ans=0
while i<len(h):
while j<len(m) and m[j]<h[i]:
j+=1
if j<len(m):
ans+=(len(m)-j)
i+=1
print(ans)
``` | output | 1 | 69,269 | 0 | 138,539 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal". | instruction | 0 | 69,270 | 0 | 138,540 |
Tags: implementation, strings, two pointers
Correct Solution:
```
def finder(S, t):
n = len(S)
answer = []
i1 = 0
for i in range(n):
if S[i]==t[i1]:
i1+=1
if i1==len(t):
i1=0
answer.append(i-len(t)+1)
else:
i1=0
if S[i]==t[i1]:
i1+=1
return answer
def process(S):
heavy = finder(S, 'heavy')
metal = finder(S, 'metal')
answer = 0
# print(heavy, metal)
i1 = 0
i2 = 0
n = len(heavy)
m = len(metal)
while i1 < n and i2 < m:
if heavy[i1] < metal[i2]:
answer+=(len(metal)-i2)
i1+=1
else:
i2+=1
return answer
S = input()
print(process(S))
``` | output | 1 | 69,270 | 0 | 138,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal". | instruction | 0 | 69,271 | 0 | 138,542 |
Tags: implementation, strings, two pointers
Correct Solution:
```
s=str(input())
s=s[::-1]
count=0
ans=0
for i in range(0,len(s)):
if s[i:i+5]=="latem":
count+=1
if s[i:i+5]=="yvaeh":
ans+=count
print(ans)
``` | output | 1 | 69,271 | 0 | 138,543 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal". | instruction | 0 | 69,272 | 0 | 138,544 |
Tags: implementation, strings, two pointers
Correct Solution:
```
z=r=0
for w in input().split("heavy"):
r+=w.count("metal")*z
z+=1
print(r)
``` | output | 1 | 69,272 | 0 | 138,545 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
Submitted Solution:
```
i, res = 0, 0
for w in input().split("heavy"):
res += w.count("metal") * i
i += 1
print(res)
``` | instruction | 0 | 69,273 | 0 | 138,546 |
Yes | output | 1 | 69,273 | 0 | 138,547 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
Submitted Solution:
```
print(sum(i*s.count("metal") for i, s in enumerate(input().split("heavy"))))
``` | instruction | 0 | 69,274 | 0 | 138,548 |
Yes | output | 1 | 69,274 | 0 | 138,549 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
Submitted Solution:
```
s = input()
heavy = 0
total = 0
i = 0
while i < len(s) - 4:
if s[i:i + 5] == 'heavy':
i += 5
heavy += 1
elif s[i:i + 5] == 'metal':
i += 5
total += heavy
else:
i += 1
print("%d" % total)
``` | instruction | 0 | 69,275 | 0 | 138,550 |
Yes | output | 1 | 69,275 | 0 | 138,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
Submitted Solution:
```
from sys import stdin, stdout
import math
import bisect
s = stdin.readline().strip()
heavy = []
metal = []
for i in range(len(s) - 5 + 1):
if s[i] not in ['m', 'h']:
continue
tmp = s[i:i + 5]
if tmp == 'heavy':
heavy.append(i)
elif tmp == 'metal':
metal.append(i)
ans = 0
for i in heavy:
ans += len(metal) - bisect.bisect(metal, i)
stdout.writelines(str(ans))
``` | instruction | 0 | 69,276 | 0 | 138,552 |
Yes | output | 1 | 69,276 | 0 | 138,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
Submitted Solution:
```
s=input()
a=s.count("heavy")
b=s.count("metal")
c=0
for i in range(1,a+1):
c+=b
b-=1
print(c)
``` | instruction | 0 | 69,277 | 0 | 138,554 |
No | output | 1 | 69,277 | 0 | 138,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
Submitted Solution:
```
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import insort, bisect_right, bisect_left
import threading, sys
def main():
s = input()
a, c, b = [], 0, []
try:
while True:
a.append(s.index("heavy",c))
c = a[-1]+1
except ValueError:
pass
try:
c = 0
while True:
b.append(s.index("metal",c))
c = b[-1] + 1
except ValueError:
pass
prev = 0
ans = [0]*len(b)
for i in range(len(b)):
lo,w = 0,prev
for j in range(prev,len(a)):
if a[j] < b[i]:
lo += 1
else:
prev = j
break
prev = max(prev,w+1)
ans[i] = ans[i-1] + ans[i-1] + lo
print(ans[-1])
if __name__ == "__main__":
"""sys.setrecursionlimit(400000)
threading.stack_size(40960000)"""
thread = threading.Thread(target=main)
thread.start()
``` | instruction | 0 | 69,278 | 0 | 138,556 |
No | output | 1 | 69,278 | 0 | 138,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
Submitted Solution:
```
s = input()
h = "heavy"
m = "metal"
sum = 0
cnt_b = 0
index_h = 1
last_index_h = 0
if h and m in s:
for i in range(s.count(h)):
last_index_h = index_h
if s[last_index_h + 4:].find(m) != -1:
index_h = s[last_index_h - 1:].find(h)
sum += s[index_h + len(h) - 1:].count(m)
index_h += len(h) + last_index_h
else:
break
print(sum)
``` | instruction | 0 | 69,279 | 0 | 138,558 |
No | output | 1 | 69,279 | 0 | 138,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
Input
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Output
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
Note
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
Submitted Solution:
```
def bigger(x,a,n):
for i in range(n):
if(a[i] > x):
break
return (n-i)
import re
s = input()
x = 'heavy'
y = 'metal'
s1 = [s.start() for s in re.finditer(x,s)]
s2 = [s.start() for s in re.finditer(y,s)]
total = 0
n = len(s2)
for i in s1:
total += bigger(i,s2,n)
print(total)
``` | instruction | 0 | 69,280 | 0 | 138,560 |
No | output | 1 | 69,280 | 0 | 138,561 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | instruction | 0 | 69,419 | 0 | 138,838 |
Tags: dp, greedy, math
Correct Solution:
```
n = int(input())
s = input()
print(min(n,sum(s[i]!=s[i-1] for i in range(1,n))+3))
``` | output | 1 | 69,419 | 0 | 138,839 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | instruction | 0 | 69,420 | 0 | 138,840 |
Tags: dp, greedy, math
Correct Solution:
```
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2016 missingdays <missingdays@missingdays>
#
# Distributed under terms of the MIT license.
"""
"""
n = int(input())
s = input()
res = 1
for i in range(1, n):
if s[i] != s[i-1]:
res += 1
print(min(res+2, n))
``` | output | 1 | 69,420 | 0 | 138,841 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | instruction | 0 | 69,421 | 0 | 138,842 |
Tags: dp, greedy, math
Correct Solution:
```
#! /bin/python
n = int(input())
tab = str(input())
d = 1
tmp = 1
changes = 1
for i in range(1, n):
if tab[i] != tab[i - 1]:
changes += 1
print(min(changes + 2, n))
``` | output | 1 | 69,421 | 0 | 138,843 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | instruction | 0 | 69,422 | 0 | 138,844 |
Tags: dp, greedy, math
Correct Solution:
```
from sys import stdin,stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int,stdin.readline().split()))
for _ in range(1):#nmbr()):
n=nmbr()
a=[int(ch) for ch in input()]
n=len(a)
dp=[[[0 for _ in range(2)]for _ in range(3)]for _ in range(n)]
dp[0][0][a[0]]=1
dp[0][0][1^a[0]]=0
dp[0][1][a[0]]=1
dp[0][1][1^a[0]]=1
dp[0][2][a[0]]=1
dp[0][2][1^a[0]]=1
for i in range(1,n):
dp[i][0][a[i]] = max(1+dp[i-1][0][1^a[i]],dp[i-1][0][a[i]])
dp[i][0][1 ^ a[i]] = 0#dp[i-1][0][1^a[i]]
dp[i][1][a[i]] = max(dp[i-1][1][a[i]],dp[i-1][0][a[i]],1+dp[i-1][0][1^a[i]])
dp[i][1][1 ^ a[i]] = max(dp[i-1][1][a[i]]+1,dp[i-1][0][a[i]]+1,dp[i-1][0][1^a[i]])
dp[i][2][a[i]] = max(dp[i-1][2][1^a[i]]+1,dp[i-1][1][1^a[i]])
dp[i][2][1 ^ a[i]] = dp[i-1][1][a[i]]+1
ans=0
# print(*dp,sep='\n')
for i in range(3):
for j in range(2):
ans=max(ans,dp[n-1][i][j])
print(ans)
``` | output | 1 | 69,422 | 0 | 138,845 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | instruction | 0 | 69,423 | 0 | 138,846 |
Tags: dp, greedy, math
Correct Solution:
```
from math import *
from collections import *
from random import *
from decimal import Decimal
from bisect import *
import sys
#input=sys.stdin.readline
def lis():
return list(map(int,input().split()))
def ma():
return map(int,input().split())
def inp():
return int(input())
def st():
return input().rstrip('\n')
n=inp()
s=st()+'$'
r=[]
co=0
p=s[0]
for i in range(n+1):
if(p==s[i]):
co+=1
else:
r.append(co)
co=1
p=s[i]
re=len(r)
fl=0
for i in range(len(r)):
if(r[i]!=1):
fl=2
break
print(min(n,re+fl))
``` | output | 1 | 69,423 | 0 | 138,847 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | instruction | 0 | 69,424 | 0 | 138,848 |
Tags: dp, greedy, math
Correct Solution:
```
from sys import maxsize, stdout, stdin,stderr
mod = int(1e9+7)
import sys
def I(): return int(stdin.readline())
def lint(): return [int(x) for x in stdin.readline().split()]
def S(): return input().strip()
def grid(r, c): return [lint() for i in range(r)]
from collections import defaultdict, Counter, deque
import math
import heapq
from heapq import heappop , heappush
import bisect
from itertools import groupby
def gcd(a,b):
while b:
a %= b
tmp = a
a = b
b = tmp
return a
def lcm(a,b):
return a // gcd(a, b) * b
def check_prime(n):
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
def Bs(a, x):
i=0
j=0
left = 1
right = x
flag=False
while left<right:
mi = (left+right)//2
#print(smi,a[mi],x)
if a[mi]<=x:
left = mi+1
i+=1
else:
right = mi
j+=1
#print(left,right,"----")
#print(i-1,j)
if left>0 and a[left-1]==x:
return i-1, j
else:
return -1, -1
def nCr(n, r):
return (fact(n) // (fact(r)
* fact(n - r)))
# Returns factorial of n
def fact(n):
res = 1
for i in range(2, n+1):
res = res * i
return res
def primefactors(n):
num=0
while n % 2 == 0:
num+=1
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
num+=1
n = n // i
if n > 2:
num+=1
return num
'''
def iter_ds(src):
store=[src]
while len(store):
tmp=store.pop()
if not vis[tmp]:
vis[tmp]=True
for j in ar[tmp]:
store.append(j)
'''
def ask(a):
print('? {}'.format(a),flush=True)
n=I()
return n
def dfs(i,p):
a,tmp=0,0
for j in d[i]:
if j!=p:
a+=1
tmp+=dfs(j,i)
if a==0:
return 0
return tmp/a + 1
def primeFactors(n):
l=[]
while n % 2 == 0:
l.append(2)
n = n // 2
if n > 2:
l.append(n)
return l
n = I()
s = list(map(int,input().strip()))
i,cnt=0,0
tmp=s[0]
ans=1
for i in range(1,n):
if s[i]!=tmp:
ans+=1
tmp=s[i]
print(min(ans+2,n))
``` | output | 1 | 69,424 | 0 | 138,849 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | instruction | 0 | 69,425 | 0 | 138,850 |
Tags: dp, greedy, math
Correct Solution:
```
n=int(input())
b=input()
print(min(n,3+sum(x!=y for x,y in zip(b,b[1:]))))
``` | output | 1 | 69,425 | 0 | 138,851 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | instruction | 0 | 69,426 | 0 | 138,852 |
Tags: dp, greedy, math
Correct Solution:
```
ans = 1
n = int(input())
s = input()
for i in range(1, n):
ans += int(s[i]!=s[i-1])
print(min(n, ans+2))
``` | output | 1 | 69,426 | 0 | 138,853 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.