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 |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from b and make it empty.
Subsequence of string s is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string s.
Input
The first line contains string a, and the second line β string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters.
Output
On the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters.
If the answer consists of zero characters, output Β«-Β» (a minus sign).
Examples
Input
hi
bob
Output
-
Input
abca
accepted
Output
ac
Input
abacaba
abcdcba
Output
abcba
Note
In the first example strings a and b don't share any symbols, so the longest string that you can get is empty.
In the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b.
Submitted Solution:
```
a = input()
b = input()
p = [None] * (len(b) + 1)
s = [None] * (len(b) + 1)
j = 0
p[0] = -1
for i in range(len(b)):
while j < len(a) and a[j] != b[i]:
j += 1
if j >= len(a):
break
else:
p[i + 1] = j
j += 1
j = len(a) - 1
s[-1] = len(b)
for i in range(len(b) - 1, -1, -1):
while j >= 0 and a[j] != b[i]:
j -= 1
if j < 0:
break
else:
s[i] = j
j -= 1
ans = ""
for i in range(len(b) + 1):
if p[i] == None:
break
else:
l = i - 1
r = len(b)
while l + 1 < r:
mid = (l + r) // 2
if s[mid] != None and p[i] < s[mid]:
r = mid
else:
l = mid
if len(ans) < i + len(b) - r:
ans = b[:i] + b[r:]
if ans == "":
print("-")
else:
print(ans)
``` | instruction | 0 | 90,591 | 0 | 181,182 |
Yes | output | 1 | 90,591 | 0 | 181,183 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from b and make it empty.
Subsequence of string s is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string s.
Input
The first line contains string a, and the second line β string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters.
Output
On the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters.
If the answer consists of zero characters, output Β«-Β» (a minus sign).
Examples
Input
hi
bob
Output
-
Input
abca
accepted
Output
ac
Input
abacaba
abcdcba
Output
abcba
Note
In the first example strings a and b don't share any symbols, so the longest string that you can get is empty.
In the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b.
Submitted Solution:
```
import sys
s = list(map(lambda c: ord(c)-97, input()))
t = list(map(lambda c: ord(c)-97, input()))
n, m = len(s), len(t)
next_c = [[-1]*26 for _ in range(n+1)]
for i in range(n-1, -1, -1):
for j in range(26):
next_c[i][j] = next_c[i+1][j]
next_c[i][s[i]] = i+1
minf = -(10**9)
dp = [[minf, minf, minf] for _ in range(m+1)]
dp_i = [[0, 0, 0] for _ in range(m+1)]
dp[0][0] = 0
def solve1(i1, j1, i2, j2, c):
next_i = next_c[dp_i[i2][j2]][c]
if next_i != -1 and (dp[i1][j1] < dp[i2][j2]+1 or dp[i1][j1] == dp[i2][j2]+1 and dp_i[i1][j1] > next_i):
dp[i1][j1] = dp[i2][j2]+1
dp_i[i1][j1] = next_i
def solve2(i1, j1, i2, j2):
if dp[i1][j1] < dp[i2][j2] or dp[i1][j1] == dp[i2][j2] and dp_i[i1][j1] > dp_i[i2][j2]:
dp[i1][j1] = dp[i2][j2]
dp_i[i1][j1] = dp_i[i2][j2]
for i in range(m):
solve1(i+1, 0, i, 0, t[i])
solve2(i+1, 1, i, 0)
solve2(i+1, 1, i, 1)
solve1(i+1, 2, i, 1, t[i])
solve1(i+1, 2, i, 2, t[i])
i, j, max_len = 0, 0, 0
for jj in range(3):
if max_len < dp[m][jj]:
i, j, max_len = m, jj, dp[m][jj]
ans = []
while i > 0:
if j == 1 and dp[i-1][j] == dp[i][j] and dp_i[i-1][j] == dp_i[i][j]:
i -= 1
continue
if j == 1 and dp[i-1][j-1] == dp[i][j] and dp_i[i-1][j-1] == dp_i[i][j]:
i -= 1
j -= 1
continue
if j != 1 and dp[i-1][j]+1 == dp[i][j] and dp_i[i-1][j] < dp_i[i][j]:
ans.append(chr(97 + t[i-1]))
i -= 1
continue
if j == 2 and dp[i-1][j-1]+1 == dp[i][j] and dp_i[i-1][j-1] < dp_i[i][j]:
ans.append(chr(97 + t[i-1]))
i -= 1
j -= 1
print(''.join(reversed(ans)) if ans else '-')
``` | instruction | 0 | 90,592 | 0 | 181,184 |
No | output | 1 | 90,592 | 0 | 181,185 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from b and make it empty.
Subsequence of string s is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string s.
Input
The first line contains string a, and the second line β string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters.
Output
On the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters.
If the answer consists of zero characters, output Β«-Β» (a minus sign).
Examples
Input
hi
bob
Output
-
Input
abca
accepted
Output
ac
Input
abacaba
abcdcba
Output
abcba
Note
In the first example strings a and b don't share any symbols, so the longest string that you can get is empty.
In the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b.
Submitted Solution:
```
a=input()
b=input()
aa=list(a)
bb=list(b)
cc=[]
for i in range(len(aa)):
for j in range(len(bb)):
if aa[i]==bb[j]:
cc.append(aa[i])
bb.pop(j)
break
if len(cc)==0:
print('-')
else:
for i in range(len(b)):
for j in range(len(cc)):
if b[i]==cc[j]:
print(b[i],end='')
cc.pop(j)
break
``` | instruction | 0 | 90,593 | 0 | 181,186 |
No | output | 1 | 90,593 | 0 | 181,187 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from b and make it empty.
Subsequence of string s is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string s.
Input
The first line contains string a, and the second line β string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters.
Output
On the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters.
If the answer consists of zero characters, output Β«-Β» (a minus sign).
Examples
Input
hi
bob
Output
-
Input
abca
accepted
Output
ac
Input
abacaba
abcdcba
Output
abcba
Note
In the first example strings a and b don't share any symbols, so the longest string that you can get is empty.
In the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b.
Submitted Solution:
```
a=str(input())
b=str(input())
counter=0
i=0
j=0
while i<len(b):
if(a[j]!=b[i] ):
k=j
while True:
if(k>len(a)-1):
break
if(a[k]==b[i]):
break
k+=1
if(k==len(a)):
newstr = b.replace(b[i], "")
b=newstr
counter+=1
i+=1
j+=1
if(not(j<len(a))):
break
if(counter==len(a)-1):
print("_")
else:
print(b)
``` | instruction | 0 | 90,594 | 0 | 181,188 |
No | output | 1 | 90,594 | 0 | 181,189 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from b and make it empty.
Subsequence of string s is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string s.
Input
The first line contains string a, and the second line β string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters.
Output
On the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters.
If the answer consists of zero characters, output Β«-Β» (a minus sign).
Examples
Input
hi
bob
Output
-
Input
abca
accepted
Output
ac
Input
abacaba
abcdcba
Output
abcba
Note
In the first example strings a and b don't share any symbols, so the longest string that you can get is empty.
In the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b.
Submitted Solution:
```
import sys
s = list(map(lambda c: ord(c)-97, input()))
t = list(map(lambda c: ord(c)-97, input()))
n, m = len(s), len(t)
i = n-1
ans1 = []
for j in range(m-1, -1, -1):
while 0 <= i and s[i] != t[j]:
i -= 1
if i < 0:
break
ans1.append(chr(97 + t[j]))
i -= 1
next_c = [[-1]*26 for _ in range(n+1)]
for i in range(n-1, -1, -1):
for j in range(26):
next_c[i][j] = next_c[i+1][j]
next_c[i][s[i]] = i+1
minf = -(10**9)
dp = [[minf, 0, minf] for _ in range(m+1)]
dp_i = [[0, 0, 0] for _ in range(m+1)]
dp[0][0] = 0
prev = [[(0, 0, 0), (0, 0, 0), (0, 0, 0)] for _ in range(m+1)]
def solve1(i1, j1, i2, j2, c):
next_i = next_c[dp_i[i2][j2]][c]
if next_i != -1 and (dp[i1][j1] < dp[i2][j2]+1 or dp[i1][j1] == dp[i2][j2]+1 and dp_i[i1][j1] > next_i):
dp[i1][j1] = dp[i2][j2]+1
dp_i[i1][j1] = next_i
prev[i1][j1] = (i2, j2, 1)
def solve2(i1, j1, i2, j2):
if dp[i1][j1] < dp[i2][j2] or dp[i1][j1] == dp[i2][j2] and dp_i[i1][j1] > dp_i[i2][j2]:
dp[i1][j1] = dp[i2][j2]
dp_i[i1][j1] = dp_i[i2][j2]
prev[i1][j1] = (i2, j2, 0)
for i in range(m):
solve1(i+1, 0, i, 0, t[i])
solve2(i+1, 1, i, 0)
solve2(i+1, 1, i, 1)
solve1(i+1, 2, i, 1, t[i])
solve1(i+1, 2, i, 2, t[i])
i, j, max_len = 0, 0, 0
for jj in range(3):
if max_len < dp[m][jj]:
i, j, max_len = m, jj, dp[m][jj]
ans = []
while i > 0:
i, j, flag = prev[i][j]
if flag:
ans.append(chr(97 + t[i]))
if len(ans1) > len(ans):
ans = ''.join(reversed(ans1))
else:
ans = ''.join(reversed(ans))
print(ans if ans else '-')
``` | instruction | 0 | 90,595 | 0 | 181,190 |
No | output | 1 | 90,595 | 0 | 181,191 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings. | instruction | 0 | 90,968 | 0 | 181,936 |
Tags: implementation, strings
Correct Solution:
```
# A. Even Substrings
n, s = int(input()), input()
ans = 0
for index, num in enumerate(s, start=1):
if int(num) % 2 == 0:
ans += index
print(ans)
``` | output | 1 | 90,968 | 0 | 181,937 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings. | instruction | 0 | 90,969 | 0 | 181,938 |
Tags: implementation, strings
Correct Solution:
```
n = int(input())
a = list(map(int, list(input())))
result = 0
for i in range(n):
if a[i] % 2 == 0:
result += i+1
print(result)
``` | output | 1 | 90,969 | 0 | 181,939 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings. | instruction | 0 | 90,970 | 0 | 181,940 |
Tags: implementation, strings
Correct Solution:
```
# cook your dish here
n=int(input())
s=input()
c=0
for i in range(0,n):
if int(s[n-i-1])%2==0:
c=c+(n-i)
print(c)
``` | output | 1 | 90,970 | 0 | 181,941 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings. | instruction | 0 | 90,971 | 0 | 181,942 |
Tags: implementation, strings
Correct Solution:
```
n=int(input())
l=input()
value=0
for i in range (n) :
if (int(l[i]))%2 == 0 :
value+=i+1
print(value)
``` | output | 1 | 90,971 | 0 | 181,943 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings. | instruction | 0 | 90,972 | 0 | 181,944 |
Tags: implementation, strings
Correct Solution:
```
from sys import stdin,stdout
from collections import defaultdict,Counter,deque
from bisect import bisect,bisect_left
import math
from itertools import permutations
import queue
#stdin = open('input.txt','r')
I = stdin.readline
n = int(I())
s = I()
count = 0
for i in range(n):
if(int(s[i])%2==0):
count+=(i+1)
print(count)
``` | output | 1 | 90,972 | 0 | 181,945 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings. | instruction | 0 | 90,973 | 0 | 181,946 |
Tags: implementation, strings
Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 23 17:40:46 2019
@author: screamLab
"""
n = int(input())
s = input()
ans = 0
for i in range(n):
if int(s[i]) % 2 == 0:
ans += i + 1
print(ans)
``` | output | 1 | 90,973 | 0 | 181,947 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings. | instruction | 0 | 90,974 | 0 | 181,948 |
Tags: implementation, strings
Correct Solution:
```
num = int(input())
datos = input()
string = list(datos)
string = list(map(int, string))
suma = 0
for n in range(num):
if string[n]%2 == 0:
suma += 1+ 1*n
print(suma)
``` | output | 1 | 90,974 | 0 | 181,949 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings. | instruction | 0 | 90,975 | 0 | 181,950 |
Tags: implementation, strings
Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [tuple(map(int, l.split())) for l in sys.stdin]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
n = I()
s = S()
r = 0
for i in range(n):
if int(s[i]) % 2 == 0:
r += i + 1
return r
print(main())
``` | output | 1 | 90,975 | 0 | 181,951 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings.
Submitted Solution:
```
def e(s):
c=0
for i in range(len(s)):
t=ord(s[i])-ord('0')
if (t%2==0):
c+=(i+1)
return c
n=int(input())
s=input()
lis=[]
for i in s:
lis.append(i)
print(e(lis))
``` | instruction | 0 | 90,976 | 0 | 181,952 |
Yes | output | 1 | 90,976 | 0 | 181,953 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings.
Submitted Solution:
```
n = int(input())
s = input()
k=0
for i in range(n):
if int(s[i])%2==0:
k+=1
k+=i
print(k)
``` | instruction | 0 | 90,977 | 0 | 181,954 |
Yes | output | 1 | 90,977 | 0 | 181,955 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings.
Submitted Solution:
```
t=int(input())
m=input()
ans=0
for i in range(t):
if(int(m[i])%2==0):
ans+=i+1
print(ans)
``` | instruction | 0 | 90,978 | 0 | 181,956 |
Yes | output | 1 | 90,978 | 0 | 181,957 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings.
Submitted Solution:
```
s=int(input())
n=input()
n=list(n)
count=0
k=[]
for i in range(len(n)):
if(int(n[i])%2==0):
count=count+(i+1)
print(count)
``` | instruction | 0 | 90,979 | 0 | 181,958 |
Yes | output | 1 | 90,979 | 0 | 181,959 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings.
Submitted Solution:
```
n = int(input())
s = input()
k = 0
for i in range(n):
if int(s[i]) // 2 == 0:
k = k + i + 1
print(k)
``` | instruction | 0 | 90,980 | 0 | 181,960 |
No | output | 1 | 90,980 | 0 | 181,961 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings.
Submitted Solution:
```
import sys
n = int(sys.stdin.readline().strip())
s = list(map(int,sys.stdin.readline().strip().split()))
x = 0
for i in range(len(s)):
if s[i]%2 == 0:
x +=i+1
sys.stdout.write(str(x)+'\n')
``` | instruction | 0 | 90,981 | 0 | 181,962 |
No | output | 1 | 90,981 | 0 | 181,963 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings.
Submitted Solution:
```
ran = int(input())
a = list(map(int,input().split(" ")))
hap = 0
for i in range(ran - 1,0):
if (a[i] % 2 == 0):
hap += (i+1)
i -= 1
print(hap,end = '')
``` | instruction | 0 | 90,982 | 0 | 181,964 |
No | output | 1 | 90,982 | 0 | 181,965 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} β¦ s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.
Input
The first line contains an integer n (1 β€ n β€ 65000) β the length of the string s.
The second line contains a string s of length n. The string s consists only of digits 1, 2, ..., 9.
Output
Print the number of even substrings of s.
Examples
Input
4
1234
Output
6
Input
4
2244
Output
10
Note
In the first example, the [l, r] pairs corresponding to even substrings are:
* s[1 ... 2]
* s[2 ... 2]
* s[1 ... 4]
* s[2 ... 4]
* s[3 ... 4]
* s[4 ... 4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1 ... 1] and s[2 ... 2] both define the substring "2", they are still counted as different substrings.
Submitted Solution:
```
def main():
res = 0
s = list(map(int, input().strip()))
for i, c in enumerate(s):
if c % 2 == 0:
res += (i+1)
print(res)
if __name__ == '__main__':
main()
``` | instruction | 0 | 90,983 | 0 | 181,966 |
No | output | 1 | 90,983 | 0 | 181,967 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have two strings a and b of equal even length n consisting of characters 0 and 1.
We're in the endgame now. To finally make the universe perfectly balanced, you need to make strings a and b equal.
In one step, you can choose any prefix of a of even length and reverse it. Formally, if a = a_1 a_2 β¦ a_n, you can choose a positive even integer p β€ n and set a to a_p a_{p-1} β¦ a_1 a_{p+1} a_{p+2} β¦ a_n.
Find a way to make a equal to b using at most n + 1 reversals of the above kind, or determine that such a way doesn't exist. The number of reversals doesn't have to be minimized.
Input
The first line contains a single integer t (1 β€ t β€ 2000), denoting the number of test cases.
Each test case consists of two lines. The first line contains a string a of length n, and the second line contains a string b of the same length (2 β€ n β€ 4000; n mod 2 = 0). Both strings consist of characters 0 and 1.
The sum of n over all t test cases doesn't exceed 4000.
Output
For each test case, if it's impossible to make a equal to b in at most n + 1 reversals, output a single integer -1.
Otherwise, output an integer k (0 β€ k β€ n + 1), denoting the number of reversals in your sequence of steps, followed by k even integers p_1, p_2, β¦, p_k (2 β€ p_i β€ n; p_i mod 2 = 0), denoting the lengths of prefixes of a to be reversed, in chronological order.
Note that k doesn't have to be minimized. If there are many solutions, output any of them.
Example
Input
4
0100011011
1101011000
10101010
10101010
0011
1001
100011
110010
Output
3
6 4 10
0
-1
7
2 6 2 6 2 2 6
Note
In the first test case, string a changes as follows:
* after the first reversal: 1000101011;
* after the second reversal: 0001101011;
* after the third reversal: 1101011000. | instruction | 0 | 91,017 | 0 | 182,034 |
Tags: constructive algorithms
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
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")
def solve_case():
a = [int(x) for x in input()]
b = [int(x) for x in input()]
n = len(a)
a_rev = []
b_rev = []
def rev_a(x):
if not x:
return
nonlocal a
a = a[:x][::-1] + a[x:]
a_rev.append(x)
def rev_b(x):
if not x:
return
nonlocal b
b = b[:x][::-1] + b[x:]
b_rev.append(x)
def answer():
#cleaning up
nonlocal a_rev
nonlocal b_rev
b_rev.reverse()
a_rev += b_rev
while True:
final = []
i = 0
repl = False
while i < len(a_rev):
if i < len(a_rev) - 1 and a_rev[i] == a_rev[i + 1]:
repl = True
i += 2
else:
final.append(a_rev[i])
i += 1
a_rev = final
if not repl:
break
print(len(a_rev))
print(*a_rev)
a_occ = [[0, 0], [0, 0]]
b_occ = [[0, 0], [0, 0]]
for i in range(0, n, 2):
a_occ[a[i]][a[i + 1]] += 1
for i in range(0, n, 2):
b_occ[b[i]][b[i + 1]] += 1
if a_occ[0][0] != b_occ[0][0] or a_occ[1][1] != b_occ[1][1]:
print(-1)
return
balanced = a_occ[0][1] == b_occ[1][0]
if not balanced:
zero, one = 0, 0
for i in range(0, n, 2):
if a[i] + a[i + 1] == 1:
zero, one = zero + a[i], one + a[i + 1]
if zero + (a_occ[0][1] - one) == b_occ[1][0]:
balanced = True
rev_a(i + 2)
break
if not balanced:
zero, one = 0, 0
for i in range(0, n, 2):
if b[i] + b[i + 1] == 1:
zero, one = zero + b[i], one + b[i + 1]
if zero + (b_occ[0][1] - one) == a_occ[1][0]:
balanced = True
rev_b(i + 2)
break
for i in range(0, n, 2):
for j in range(i, n, 2):
if a[j] == b[n - i - 1] and a[j + 1] == b[n - i - 2]:
rev_a(j)
rev_a(j + 2)
break
answer()
def main():
for _ in range(int(input())):
solve_case()
main()
``` | output | 1 | 91,017 | 0 | 182,035 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total. | instruction | 0 | 91,130 | 0 | 182,260 |
Tags: combinatorics, dp, strings
Correct Solution:
```
from collections import defaultdict as dd
mod = 10**9 + 7
pw = {-1:0, 0:1}
for i in range(1,200001):
pw[i] = pw[i-1]*3 % mod
def solve(st, n):
a,c,q = dd(int), dd(int), dd(int)
for i,it in enumerate(st):
a[i], c[i], q[i] = a[i-1], c[i-1], q[i-1]
if it == 'a':
a[i]+=1
elif it == 'c':
c[i]+=1
elif it == '?':
q[i]+=1
ans = 0
for i in range(n):
if st[i] in '?b':
ca, q1 = a[i-1], q[i-1]
cc, q2 = c[n-1] - c[i], q[n-1] - q[i]
ta = ( ca*pw[q1]% mod + q1*pw[q1-1]% mod ) % mod
tc = ( cc*pw[q2]% mod + q2*pw[q2-1]% mod ) % mod
ans += ta * tc
ans %= mod
return ans
n = int(input())
st = input()
ans = solve(st, n)
print(ans)
``` | output | 1 | 91,130 | 0 | 182,261 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total. | instruction | 0 | 91,131 | 0 | 182,262 |
Tags: combinatorics, dp, strings
Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
# sys.setrecursionlimit(5*10**5)
inf = 10**20
mod = 10**9 + 7
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return list(input().split())
def S(): return input()
n = II()
s = S()
pow_calc = [1 for _ in range(n+1)]
for i in range(1,n+1):
pow_calc[i] = pow_calc[i-1] * 3
pow_calc[i] %= mod
def solve():
la = lq = rc = rq = 0
ans = 0
for char in s:
if char == 'c': rc += 1
if char == '?': rq += 1
for char in s:
if char == 'c': rc -= 1
if char == '?': rq -= 1
if char in {'?', 'b'}:
l = (la * pow_calc[lq]) % mod + (lq * pow_calc[lq-1]) % mod
l %= mod
r = (rc * pow_calc[rq]) % mod + (rq * pow_calc[rq-1]) % mod
r %= mod
ans += (l * r) % mod
ans %= mod
if char == 'a':
la += 1
if char == '?':
lq += 1
return ans
def main():
ans = solve()
print(ans)
return 0
main()
``` | output | 1 | 91,131 | 0 | 182,263 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total. | instruction | 0 | 91,132 | 0 | 182,264 |
Tags: combinatorics, dp, strings
Correct Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
s = input()[:-1]
dp = [[0]*4 for _ in range(n+1)]
dp[0][0] = 1
MOD = 10**9+7
for i in range(n):
if s[i]!='?':
dp[i+1][0] = dp[i][0]
dp[i+1][1] = (dp[i][1]+(dp[i][0] if s[i]=='a' else 0))%MOD
dp[i+1][2] = (dp[i][2]+(dp[i][1] if s[i]=='b' else 0))%MOD
dp[i+1][3] = (dp[i][3]+(dp[i][2] if s[i]=='c' else 0))%MOD
else:
dp[i+1][0] = 3*dp[i][0]%MOD
dp[i+1][1] = (dp[i][0]+3*dp[i][1])%MOD
dp[i+1][2] = (dp[i][1]+3*dp[i][2])%MOD
dp[i+1][3] = (dp[i][2]+3*dp[i][3])%MOD
print(dp[n][3])
``` | output | 1 | 91,132 | 0 | 182,265 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total. | instruction | 0 | 91,133 | 0 | 182,266 |
Tags: combinatorics, dp, strings
Correct Solution:
```
n = int(input())
word = input()
P = [[0, 0, 0] for _ in range(n + 1)]
P_w = [0, 0, 0]
cnt = 1
mod = int(1e9 + 7)
for i in range(n):
P[i+1][0] = P[i][0]
P[i+1][1] = P[i][1]
P[i+1][2] = P[i][2]
if word[i] == "a":
P[i+1][0] = (P[i][0] + cnt) % mod
elif word[i] == "b":
P[i+1][1] = (P[i][1] + P[i][0]) % mod
elif word[i] == "c":
P[i+1][2] = (P[i][2] + P[i][1]) % mod
elif word[i] == "?":
P[i+1][2] = (3 * P[i][2] + P[i][1]) % mod
P[i+1][1] = (3 * P[i][1] + P[i][0]) % mod
P[i+1][0] = (3 * P[i][0] + cnt) % mod
cnt = (cnt * 3) % mod
print(P[-1][2])
``` | output | 1 | 91,133 | 0 | 182,267 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total. | instruction | 0 | 91,134 | 0 | 182,268 |
Tags: combinatorics, dp, strings
Correct Solution:
```
Mod = 10 ** 9 + 7
n = int(input())
s = input()
cnt, a, ab, abc = 1, 0, 0, 0
for i in range(len(s)):
if s[i] == 'a':
a += cnt
a %= Mod
elif s[i] == 'b':
ab += a
ab %= Mod
elif s[i] == 'c':
abc += ab
abc %= Mod
else:
abc *= 3
abc += ab
abc %= Mod
ab *= 3
ab += a
ab %= Mod
a *= 3
a += cnt
a %= Mod
cnt *= 3
cnt %= Mod
print(abc)
``` | output | 1 | 91,134 | 0 | 182,269 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total. | instruction | 0 | 91,135 | 0 | 182,270 |
Tags: combinatorics, dp, strings
Correct Solution:
```
from bisect import bisect_left, bisect_right
class Result:
def __init__(self, index, value):
self.index = index
self.value = value
class BinarySearch:
def __init__(self):
pass
@staticmethod
def greater_than(num: int, func, size: int = 1):
"""Searches for smallest element greater than num!"""
if isinstance(func, list):
index = bisect_right(func, num)
if index == len(func):
return Result(None, None)
else:
return Result(index, func[index])
else:
alpha, omega = 0, size - 1
if func(omega) <= num:
return Result(None, None)
while alpha < omega:
if func(alpha) > num:
return Result(alpha, func(alpha))
if omega == alpha + 1:
return Result(omega, func(omega))
mid = (alpha + omega) // 2
if func(mid) > num:
omega = mid
else:
alpha = mid
@staticmethod
def less_than(num: int, func, size: int = 1):
"""Searches for largest element less than num!"""
if isinstance(func, list):
index = bisect_left(func, num) - 1
if index == -1:
return Result(None, None)
else:
return Result(index, func[index])
else:
alpha, omega = 0, size - 1
if func(alpha) >= num:
return Result(None, None)
while alpha < omega:
if func(omega) < num:
return Result(omega, func(omega))
if omega == alpha + 1:
return Result(alpha, func(alpha))
mid = (alpha + omega) // 2
if func(mid) < num:
alpha = mid
else:
omega = mid
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
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")
# ------------------- fast io --------------------
from math import gcd, ceil
def pre(s):
n = len(s)
pi = [0] * n
for i in range(1, n):
j = pi[i - 1]
while j and s[i] != s[j]:
j = pi[j - 1]
if s[i] == s[j]:
j += 1
pi[i] = j
return pi
def prod(a):
ans = 1
for each in a:
ans = (ans * each)
return ans
def lcm(a, b): return a * b // gcd(a, b)
def binary(x, length=16):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
bs = BinarySearch()
for _ in range(int(input()) if not True else 1):
n = int(input())
s = input()
mod = 10**9 + 7
a = []
b = []
c = []
q = []
ans = 0
for i in range(n):
if s[i] == 'a': a+=[i]
elif s[i] == 'b': b += [i]
elif s[i] == 'c': c += [i]
else: q += [i]
q0 = pow(3, len(q), mod)
q1 = pow(3, len(q)-1, mod) if len(q)>0 else 0
q2 = pow(3, len(q)-2, mod) if len(q)>1 else 0
q3 = pow(3, len(q)-3, mod) if len(q)>2 else 0
# ab*
# abc
bc, bq = [], []
for i in b:
ind, ind2 = bs.greater_than(i, c).index, bs.greater_than(i, q).index
count = 0 if ind is None else len(c)-ind
count2 = 0 if ind2 is None else len(q)-ind2
bc += [count]
bq += [count2]
for i in range(len(bc)-2, -1, -1):
bc[i] += bc[i+1]
bq[i] += bq[i+1]
for i in a:
ind = bs.greater_than(i, b).index
if ind is None:continue
if bc[ind]:
ans += bc[ind] * q0
ans = ans % mod
if bq[ind]:
ans += bq[ind] * q1
ans = ans % mod
# *bc
# *b*
for i in q:
ind = bs.greater_than(i, b).index
if ind is None:continue
if bc[ind]:
ans += bc[ind] * q1
ans = ans % mod
if bq[ind]:
ans += bq[ind] * q2
ans = ans % mod
#a*c
#a**
bc, bq = [], []
for i in q:
ind, ind2 = bs.greater_than(i, c).index, bs.greater_than(i, q).index
count = 0 if ind is None else len(c) - ind
count2 = 0 if ind2 is None else len(q) - ind2
bc += [count]
bq += [count2]
for i in range(len(bc) - 2, -1, -1):
bc[i] += bc[i + 1]
bq[i] += bq[i + 1]
for i in a:
ind = bs.greater_than(i, q).index
if ind is None:continue
if bc[ind]:
ans += bc[ind] * q1
ans = ans % mod
if bq[ind]:
ans += bq[ind] * q2
ans = ans % mod
# **c
# ***
for i in q:
ind = bs.greater_than(i, q).index
if ind is None: continue
if bc[ind]:
ans += bc[ind] * q2
ans = ans % mod
if bq[ind]:
ans += bq[ind] * q3
ans = ans % mod
print(ans)
``` | output | 1 | 91,135 | 0 | 182,271 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total. | instruction | 0 | 91,136 | 0 | 182,272 |
Tags: combinatorics, dp, strings
Correct Solution:
```
mod=10**9+7
n=int(input())
s=input()
na=0
nb=0
nc=0
rt=1
for i in range(n):
if s[i]=='?':
nc=3*nc+nb
nb=3*nb+na
na=3*na+rt
rt*=3
else:
if s[i]=='a':
na+=rt
elif s[i]=='b':
nb+=na
else:
nc+=nb
nc%=mod
na%=mod
nb%=mod
rt%=mod
print(nc%mod)
``` | output | 1 | 91,136 | 0 | 182,273 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total. | instruction | 0 | 91,137 | 0 | 182,274 |
Tags: combinatorics, dp, strings
Correct Solution:
```
mod=10**9+7
n=int(input())
s=input()[::-1]
#dp=[c,bc,abc]
dp=[[0]*3 for _ in range(n+1)]
pows=[1]
for i in range(n):
pows.append((pows[-1]*3)%mod)
cnt=0
for i in range(n):
if s[i]=='a':
dp[i+1][0]+=dp[i][0]
dp[i+1][1]+=dp[i][1]
dp[i+1][2]+=dp[i][2]+dp[i][1]
elif s[i]=='b':
dp[i+1][0]+=dp[i][0]
dp[i+1][1]+=dp[i][1]+dp[i][0]
dp[i+1][2]+=dp[i][2]
elif s[i]=='c':
dp[i+1][0]+=dp[i][0]+pows[cnt]
dp[i+1][1]+=dp[i][1]
dp[i+1][2]+=dp[i][2]
elif s[i]=='?':
#a
dp[i+1][0]+=dp[i][0]
dp[i+1][1]+=dp[i][1]
dp[i+1][2]+=dp[i][2]+dp[i][1]
#b
dp[i+1][0]+=dp[i][0]
dp[i+1][1]+=dp[i][1]+dp[i][0]
dp[i+1][2]+=dp[i][2]
#c
dp[i+1][0]+=dp[i][0]+pows[cnt]
dp[i+1][1]+=dp[i][1]
dp[i+1][2]+=dp[i][2]
cnt+=1
dp[i+1][0]%=mod
dp[i+1][1]%=mod
dp[i+1][2]%=mod
print(dp[n][2])
``` | output | 1 | 91,137 | 0 | 182,275 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total.
Submitted Solution:
```
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.readline
N = int(input())
S = input().rstrip('\n')
a_num = [0] * (N+1)
q_num_left = [0] * (N+1)
for i, s in enumerate(S):
a_num[i+1] = a_num[i]
q_num_left[i+1] = q_num_left[i]
if s == "a":
a_num[i+1] += 1
if s == "?":
q_num_left[i+1] += 1
S_rev = S[::-1]
c_num = [0] * (N+1)
q_num_right = [0] * (N+1)
for i, s in enumerate(S_rev):
c_num[i+1] = c_num[i]
q_num_right[i+1] = q_num_right[i]
if s == "c":
c_num[i+1] += 1
if s == "?":
q_num_right[i+1] += 1
ans = 0
k = q_num_left[-1]
k0 = pow(3, k, mod)
k1 = pow(3, max(0, k-1), mod)
k2 = pow(3, max(0, k-2), mod)
k3 = pow(3, max(0, k-3), mod)
for i, s in enumerate(S):
if s == "b":
# ac
ans = (ans + ((a_num[i] * c_num[N - i - 1])%mod * k0)%mod)%mod
# ?c
ans = (ans + ((q_num_left[i] * c_num[N - i - 1])%mod * k1)%mod)%mod
# a?
ans = (ans + ((a_num[i] * q_num_right[N - i - 1])%mod * k1)%mod)%mod
# ??
ans = (ans + ((q_num_left[i] * q_num_right[N - i - 1]) % mod * k2) % mod) % mod
elif s == "?":
# ac
ans = (ans + ((a_num[i] * c_num[N - i - 1]) % mod * k1) % mod) % mod
# ?c
ans = (ans + ((q_num_left[i] * c_num[N - i - 1]) % mod * k2) % mod) % mod
# a?
ans = (ans + ((a_num[i] * q_num_right[N - i - 1]) % mod * k2) % mod) % mod
# ??
ans = (ans + ((q_num_left[i] * q_num_right[N - i - 1]) % mod * k3) % mod) % mod
print(ans)
if __name__ == '__main__':
main()
``` | instruction | 0 | 91,138 | 0 | 182,276 |
Yes | output | 1 | 91,138 | 0 | 182,277 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total.
Submitted Solution:
```
n=int(input())
s=input()
a=0
b=0
c=0
v=1
mod = 10 ** 9 + 7
for i in range(n):
if s[i] == 'a':
a += v
elif s[i]=='b':
b += a
elif s[i]=='c':
c += b
else:
c=(3 * c + b) % mod
b=(3 * b + a) % mod
a=(3 * a + v) % mod
v=(3 * v) % mod
print(c % mod)
``` | instruction | 0 | 91,139 | 0 | 182,278 |
Yes | output | 1 | 91,139 | 0 | 182,279 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total.
Submitted Solution:
```
n=int(input())
s=input()
dp=[[0,0,0]for i in range(n)]
ct=1
mod=10**9+7
for i in range(n):
if i==0:
if s[i]=='?':ct*=3
if s[i]=='a' or s[i]=='?':dp[i][0]+=1
else:
aa,bb,cc=dp[i-1]
if s[i]=='a':
dp[i][0]=aa+ct
dp[i][1]=bb
dp[i][2]=cc
if s[i]=='b':
dp[i][0]=aa
dp[i][1]=aa+bb
dp[i][2]=cc
if s[i]=='c':
dp[i][0]=aa
dp[i][1]=bb
dp[i][2]=bb+cc
if s[i]=='?':
dp[i][0]=aa*3+ct
dp[i][1]=bb*3+aa
dp[i][2]=cc*3+bb
ct*=3
ct%=mod
dp[i][0]%=mod
dp[i][1]%=mod
dp[i][2]%=mod
print(dp[-1][-1])
``` | instruction | 0 | 91,140 | 0 | 182,280 |
Yes | output | 1 | 91,140 | 0 | 182,281 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total.
Submitted Solution:
```
import math
import sys
from sys import stdin, stdout
ipi = lambda: int(stdin.readline())
ipil = lambda: map(int, stdin.readline().split())
ipf = lambda: float(stdin.readline())
ipfl = lambda: map(float, stdin.readline().split())
ips = lambda: stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + "\n")
outl = lambda x: print(*x)
n = ipi()
s = ips()
a_cnt = 0
ab_cnt = 0
qst_cnt = 0
ans = 0
mod = int(1e9 + 7)
for i in range(n):
if s[i] == 'a':
a_cnt += pow(3, qst_cnt, mod)
elif s[i] == 'b':
ab_cnt += a_cnt
elif s[i] == 'c':
ans += ab_cnt
else:
ans *= 3
ans += ab_cnt
ab_cnt *= 3
ab_cnt += a_cnt
a_cnt *= 3
a_cnt += pow(3, qst_cnt, mod)
qst_cnt += 1
a_cnt %= mod
ab_cnt %= mod
ans %= mod
print(ans)
``` | instruction | 0 | 91,141 | 0 | 182,282 |
Yes | output | 1 | 91,141 | 0 | 182,283 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total.
Submitted Solution:
```
class CombsABC:
def __init__(self):
self.ft = 0
def find_subs(self, strs):
t = 0
for a in range(len(strs)):
if strs[a] == 'a':
for b in range(a + 1, len(strs)):
if strs[b] == 'b':
for c in range(b + 1, len(strs)):
if strs[c] == 'c':
t += 1
self.ft += t
def find_str(self, s, pos, i):
if i > len(pos) - 1:
return s
a = self.find_str(s[:pos[i]] + 'a' + s[pos[i] + 1:], pos, i + 1)
if a: self.find_subs(a)
b = self.find_str(s[:pos[i]] + 'b' + s[pos[i] + 1:], pos, i + 1)
if b: self.find_subs(b)
c = self.find_str(s[:pos[i]] + 'c' + s[pos[i] + 1:], pos, i + 1)
if c: self.find_subs(c)
l = int(input())
s = input() # 'ac?b?c'
pos = [_ for _ in range(len(s)) if s[_] == '?']
ins = CombsABC()
ins.find_str(s, pos, 0)
print(ins.ft)
``` | instruction | 0 | 91,142 | 0 | 182,284 |
No | output | 1 | 91,142 | 0 | 182,285 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total.
Submitted Solution:
```
n = int(input())
S = input()
a = 0
b = 0
tot = 0
q = 0
for s in S:
if s == 'a':
a += 1
elif s == 'b':
b += a
elif s == 'c':
tot += b
else:
tot = tot*3 + b
b = b*3 + a
a = a*3 + 3**q
q += 1
print(tot%(10**9 + 7))
``` | instruction | 0 | 91,143 | 0 | 182,286 |
No | output | 1 | 91,143 | 0 | 182,287 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total.
Submitted Solution:
```
n=int(input())
s=list(input())
A,B,C=[0]*n,[0]*n,[0]*n
if s[n-1]=="c" or s[n-1]=="?":
C[-1]=1
for i in range(n-2,-1,-1):
if s[i]=="c":
C[i]=C[i+1]+1
A[i]=A[i+1]
B[i]=B[i+1]
elif s[i]=="b":
B[i]=B[i+1]+C[i+1]
C[i]=C[i+1]
A[i]=A[i+1]
elif s[i]=="a":
A[i]=A[i+1]+B[i+1]
B[i]=B[i+1]
C[i]=C[i+1]
else:
A[i]=3*A[i+1]+B[i+1]
B[i]=3*B[i+1]+C[i+1]
C[i]=3*C[i+1]+1
print(A[0])
``` | instruction | 0 | 91,144 | 0 | 182,288 |
No | output | 1 | 91,144 | 0 | 182,289 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and "c". For example, if s = "ac?b?c" then we can obtain the following strings: ["acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"].
Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo 10^{9} + 7.
A subsequence of the string t is such a sequence that can be derived from the string t after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" β a subsequence consisting of letters at positions (2, 5, 6) and a subsequence consisting of letters at positions (3, 5, 6).
Input
The first line of the input contains one integer n (3 β€ n β€ 200 000) β the length of s.
The second line of the input contains the string s of length n consisting of lowercase Latin letters "a", "b" and "c" and question marks"?".
Output
Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo 10^{9} + 7.
Examples
Input
6
ac?b?c
Output
24
Input
7
???????
Output
2835
Input
9
cccbbbaaa
Output
0
Input
5
a???c
Output
46
Note
In the first example, we can obtain 9 strings:
* "acabac" β there are 2 subsequences "abc",
* "acabbc" β there are 4 subsequences "abc",
* "acabcc" β there are 4 subsequences "abc",
* "acbbac" β there are 2 subsequences "abc",
* "acbbbc" β there are 3 subsequences "abc",
* "acbbcc" β there are 4 subsequences "abc",
* "accbac" β there is 1 subsequence "abc",
* "accbbc" β there are 2 subsequences "abc",
* "accbcc" β there are 2 subsequences "abc".
So, there are 2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24 subsequences "abc" in total.
Submitted Solution:
```
import sys
input = sys.stdin.readline
# import collections
n = int(input())
# a = list(map(int, input().split()))
s = input()
a = [0] * n
aq = [0] * n
c = [0] * n
cq = [0] * n
c1 = 0
c1q = 0
for i in range(1, len(s)):
if s[i] == "c":
c1 += 1
if s[i] == "?":
c1q += 1
c[0] = c1
cq[0] = c1q
for i in range(1, n-1):
if s[i-1] == "a":
a[i] = (a[i-1] + 1)
else:
a[i] = (a[i-1])
if s[i-1] == "?":
aq[i] = (aq[i-1] + 1)
else:
aq[i] = (aq[i-1])
if s[i] == "c":
c[i] = (c[i-1] - 1)
else:
c[i] = (c[i-1])
if s[i] == "?":
cq[i] = (cq[i-1] - 1)
else:
cq[i] = (cq[i-1])
#print(a, aq, c, cq)
sm = 0
for i in range(n):
if s[i] == "b" or s[i] == "?":
sm += (a[i] * 3**aq[i] + aq[i] * 3**(aq[i]-1)) * (c[i] * 3**cq[i] + cq[i] * 3**(cq[i]-1))
print(int(sm))
``` | instruction | 0 | 91,145 | 0 | 182,290 |
No | output | 1 | 91,145 | 0 | 182,291 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0. | instruction | 0 | 91,170 | 0 | 182,340 |
Tags: binary search, bitmasks, brute force, dp, strings, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
s = input()[:-1]
left, right = 0, n
while left < right:
mid = right - (right - left) // 2
A = [[0] * (n + 2) for _ in range(k)]
for c in range(k):
A[c][n] = A[c][n + 1] = n + 1
L = 0
for i in range(n - 1, -1, -1):
if s[i] == '?' or ord(s[i]) - ord('a') == c: L += 1
else: L = 0
A[c][i] = i + mid if L >= mid else A[c][i + 1]
dp = [n + 1] * (1 << k)
dp[0] = 0
for mask in range(1 << k):
for i in range(k):
if mask >> k & 1: continue
t = mask | 1 << i
dp[t] = min(dp[t], A[i][dp[mask]])
if dp[-1] <= n: left = mid
else: right = mid - 1
print(left)
``` | output | 1 | 91,170 | 0 | 182,341 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0. | instruction | 0 | 91,171 | 0 | 182,342 |
Tags: binary search, bitmasks, brute force, dp, strings, two pointers
Correct Solution:
```
import sys;input = sys.stdin.readline;n, k = map(int, input().split());s = input()[:-1];left, right = 0, n
while left < right:
mid = right - (right - left) // 2;A = [[0] * (n + 2) for _ in range(k)]
for c in range(k):
A[c][n] = A[c][n + 1] = n + 1;L = 0
for i in range(n - 1, -1, -1):L = (L + 1 if s[i] == '?' or ord(s[i]) - ord('a') == c else 0);A[c][i] = i + mid if L >= mid else A[c][i + 1]
dp = [n + 1] * (1 << k);dp[0] = 0
for mask in range(1 << k):
for i in range(k):
if mask >> k & 1: continue
t = mask | 1 << i;dp[t] = min(dp[t], A[i][dp[mask]])
if dp[-1] <= n: left = mid
else: right = mid - 1
print(left)
``` | output | 1 | 91,171 | 0 | 182,343 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0. | instruction | 0 | 91,172 | 0 | 182,344 |
Tags: binary search, bitmasks, brute force, dp, strings, two pointers
Correct Solution:
```
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import deque
n, k = map(int,input().split())
s = input()
def judge(needed):
inf = 2147483647
minstate = [inf]*(1<<k)
minstate[0] = 0
effect = [[inf]*(n+1) for j in range(k)]
for j in range(k):
accu = 0
index = inf
for i in range(n)[::-1]:
if s[i]==ord('?') or s[i]==97+j:
accu += 1
else:
accu = 0
if accu>=needed:
index = i + needed
effect[j][i] = index
effect[j][i] = min(effect[j][i+4-4],effect[j][i+3-3],inf)
# print(effect)
for state in range(1,1<<k):
minimum = minstate[state]
for j in range(k):
if (1<<j) & state==0: continue
index = minstate[state^(1<<j)]
if index<n:
minimum = min(minimum, effect[j][index])
minstate[state] = minimum
# print(minstate)
if minstate[-1]<=n: return True
return False
front = 0
rear = n//k+1
while front < rear:
mid = (front+rear)//2
flag = judge(mid)
# print(mid,flag)
if flag:
front = mid + 1
else:
rear = mid
print(front-1)
``` | output | 1 | 91,172 | 0 | 182,345 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0. | instruction | 0 | 91,173 | 0 | 182,346 |
Tags: binary search, bitmasks, brute force, dp, strings, two pointers
Correct Solution:
```
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import deque
n, k = map(int,input().split())
s = input()
def judge(needed):
inf = 2147483647
minstate = [inf]*(1<<k)
minstate[0] = 0
effect = [[inf]*(n+1) for j in range(k)]
for j in range(k):
accu = 0
index = inf
for i in range(n)[::-1]:
if s[i]==ord('?') or s[i]==97+j:
accu += 1
else:
accu = 0
if accu>=needed:
index = i + needed
# print(j,i,accu)
effect[j][i] = index
# print(effect)
for state in range(1,1<<k):
minimum = minstate[state]
for j in range(k):
if (1<<j) & state==0: continue
index = minstate[state^(1<<j)]
if index<n:
minimum = min(minimum, effect[j][index])
minstate[state] = minimum
# print(minstate)
if minstate[-1]<=n: return True
return False
front = 0
rear = n//k+1
while front < rear:
mid = (front+rear)//2
flag = judge(mid)
# print(mid,flag)
if flag:
front = mid + 1
else:
rear = mid
print(front-1)
``` | output | 1 | 91,173 | 0 | 182,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0. | instruction | 0 | 91,174 | 0 | 182,348 |
Tags: binary search, bitmasks, brute force, dp, strings, two pointers
Correct Solution:
```
N, K = list(map(int, input().split()))
S = input().strip()
S = [-1 if _ == '?' else ord(_) - ord('a') for _ in S]
def check(x):
p = [[N for i in range(N+1)] for k in range(K)]
for k in range(K):
keep = 0
for i in range(N-1, -1, -1):
keep += 1
if S[i] != -1 and S[i] != k:
keep = 0
p[k][i] = p[k][i+1]
if keep >= x:
p[k][i] = i + x - 1
d = [N for s in range(1<<K)]
d [0] = -1
for s in range(1, 1<<K):
for k in range(K):
if (s&(1<<k)) and (d[s^(1<<k)]<N):
d[s] = min(d[s], p[k][d[s^(1<<k)]+1])
# print('d[%d%d]=%d'%(s//2, s%2, d[s]))
return d[(1<<K)-1] < N
l, r = 0, N//K
while l < r:
mid = (l + r + 1) // 2
if check(mid):
l = mid
else:
r = mid - 1
print(l)
``` | output | 1 | 91,174 | 0 | 182,349 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0. | instruction | 0 | 91,175 | 0 | 182,350 |
Tags: binary search, bitmasks, brute force, dp, strings, two pointers
Correct Solution:
```
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import deque
n, k = map(int,input().split())
s = input()
def judge(needed):
inf = 2147483647
minstate = [inf]*(1<<k)
minstate[0] = 0
effect = [[inf]*(n+1) for j in range(k)]
for j in range(k):
accu = 0
index = inf
for i in range(n)[::-1]:
if s[i]==ord('?') or s[i]==97+j:
accu += 1
else:
accu = 0
if accu>=needed:
index = i + needed
effect[j][i] = index
# effect[j][i] = min(effect[j][i],inf*inf+inf*inf)
# print(effect)
for state in range(1,1<<k):
minimum = minstate[state]
for j in range(k):
if (1<<j) & state==0: continue
index = minstate[state^(1<<j)]
if index<n:
minimum = min(minimum, effect[j][index])
minstate[state] = minimum
# print(minstate)
if minstate[-1]<=n: return True
return False
front = 0
rear = n//k+1
while front < rear:
mid = (front+rear)//2
flag = judge(mid)
# print(mid,flag)
if flag:
front = mid + 1
else:
rear = mid
print(front-1)
``` | output | 1 | 91,175 | 0 | 182,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0. | instruction | 0 | 91,176 | 0 | 182,352 |
Tags: binary search, bitmasks, brute force, dp, strings, two pointers
Correct Solution:
```
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import deque
n, k = map(int,input().split())
s = input()
def judge(needed):
inf = 2147483647
minstate = [inf]*(1<<k)
minstate[0] = 0
effect = [[inf]*(n+1) for j in range(k)]
for j in range(k):
accu = 0
index = inf
for i in range(n)[::-1]:
if s[i]==ord('?') or s[i]==97+j:
accu += 1
else:
accu = 0
if accu>=needed:
index = i + needed
effect[j][i] = index
effect[j][i] = min(effect[j][i],inf)
# print(effect)
for state in range(1,1<<k):
minimum = minstate[state]
for j in range(k):
if (1<<j) & state==0: continue
index = minstate[state^(1<<j)]
if index<n:
minimum = min(minimum, effect[j][index])
minstate[state] = minimum
# print(minstate)
if minstate[-1]<=n: return True
return False
front = 0
rear = n//k+1
while front < rear:
mid = (front+rear)//2
flag = judge(mid)
# print(mid,flag)
if flag:
front = mid + 1
else:
rear = mid
print(front-1)
``` | output | 1 | 91,176 | 0 | 182,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0. | instruction | 0 | 91,177 | 0 | 182,354 |
Tags: binary search, bitmasks, brute force, dp, strings, two pointers
Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip()
N, K = map(int, input().split())
S = [-1 if a == "?" else ord(a) - 97 for a in input()]
II = {1 << i: i for i in range(20)}
def calc(mmm):
inf = 300000
X = [[0] * N for _ in range(K)]
for k in range(K):
Xk = X[k]
mi = inf
r = 0
for i in range(N)[::-1]:
if S[i] < 0 or S[i] == k:
r += 1
else:
r = 0
if r >= mmm:
mi = min(mi, i + mmm)
Xk[i] = mi
Y = [0] * (1 << K)
for i in range(1, 1 << K):
mi = inf
for j in range(K):
if i >> j & 1:
ii = i ^ (1 << j)
if Y[ii] < N:
mi = min(mi, X[j][Y[ii]])
Y[i] = mi
return 1 if Y[-1] < inf else 0
l, r = 0, N // K + 1
while r - l > 1:
m = l + r >> 1
if calc(m):
l = m
else:
r = m
print(l)
``` | output | 1 | 91,177 | 0 | 182,355 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0.
Submitted Solution:
```
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import deque
n, k = map(int,input().split())
s = input()
maxconseq = [[0 for j in range(k)] for i in range(n+1)]
for i in range(n):
if s[i]==ord('?'):
for j in range(k):
maxconseq[i][j] = maxconseq[i-1][j] + 1
else:
j = s[i]-97
maxconseq[i][j] = maxconseq[i-1][j] + 1
def judge(needed):
inf = 2147483647
minstate = [inf]*(1<<k)
minstate[0] = 0
effect = [[inf]*(n+1) for j in range(k)]
for j in range(k):
for i in range(n)[::-1]:
if maxconseq[i][j]>=needed:
effect[j][i-needed+1] = i+1
effect[j][i] = min(effect[j][i], effect[j][i+1])
for state in range(1,1<<k):
minimum = minstate[state]
for j in range(k):
if (1<<j) & state==0: continue
index = minstate[state^(1<<j)]
if index<n:
minimum = min(minimum, effect[j][index])
minstate[state] = minimum
# print(minstate)
if minstate[-1]<=n: return True
return False
front = 0
rear = n//k+1
while front < rear:
mid = (front+rear)//2
flag = judge(mid)
# print(mid,flag)
if flag:
front = mid + 1
else:
rear = mid
print(front-1)
``` | instruction | 0 | 91,178 | 0 | 182,356 |
Yes | output | 1 | 91,178 | 0 | 182,357 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0.
Submitted Solution:
```
def divisors(M):
d=[]
i=1
while M>=i**2:
if M%i==0:
d.append(i)
if i**2!=M:
d.append(M//i)
i=i+1
return d
def popcount(x):
x = x - ((x >> 1) & 0x55555555)
x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
x = (x + (x >> 4)) & 0x0f0f0f0f
x = x + (x >> 8)
x = x + (x >> 16)
return x & 0x0000007f
def eratosthenes(n):
res=[0 for i in range(n+1)]
prime=set([])
for i in range(2,n+1):
if not res[i]:
prime.add(i)
for j in range(1,n//i+1):
res[i*j]=1
return prime
def factorization(n):
res=[]
for p in prime:
if n%p==0:
while n%p==0:
n//=p
res.append(p)
if n!=1:
res.append(n)
return res
def euler_phi(n):
res = n
for x in range(2,n+1):
if x ** 2 > n:
break
if n%x==0:
res = res//x * (x-1)
while n%x==0:
n //= x
if n!=1:
res = res//n * (n-1)
return res
def ind(b,n):
res=0
while n%b==0:
res+=1
n//=b
return res
def isPrimeMR(n):
d = n - 1
d = d // (d & -d)
L = [2, 3, 5, 7, 11, 13, 17]
for a in L:
t = d
y = pow(a, t, n)
if y == 1: continue
while y != n - 1:
y = (y * y) % n
if y == 1 or t == n - 1: return 0
t <<= 1
return 1
def findFactorRho(n):
from math import gcd
m = 1 << n.bit_length() // 8
for c in range(1, 99):
f = lambda x: (x * x + c) % n
y, r, q, g = 2, 1, 1, 1
while g == 1:
x = y
for i in range(r):
y = f(y)
k = 0
while k < r and g == 1:
ys = y
for i in range(min(m, r - k)):
y = f(y)
q = q * abs(x - y) % n
g = gcd(q, n)
k += m
r <<= 1
if g == n:
g = 1
while g == 1:
ys = f(ys)
g = gcd(abs(x - ys), n)
if g < n:
if isPrimeMR(g): return g
elif isPrimeMR(n // g): return n // g
return findFactorRho(g)
def primeFactor(n):
i = 2
ret = {}
rhoFlg = 0
while i*i <= n:
k = 0
while n % i == 0:
n //= i
k += 1
if k: ret[i] = k
i += 1 + i % 2
if i == 101 and n >= 2 ** 20:
while n > 1:
if isPrimeMR(n):
ret[n], n = 1, 1
else:
rhoFlg = 1
j = findFactorRho(n)
k = 0
while n % j == 0:
n //= j
k += 1
ret[j] = k
if n > 1: ret[n] = 1
if rhoFlg: ret = {x: ret[x] for x in sorted(ret)}
return ret
def divisors(n):
res = [1]
prime = primeFactor(n)
for p in prime:
newres = []
for d in res:
for j in range(prime[p]+1):
newres.append(d*p**j)
res = newres
res.sort()
return res
def xorfactorial(num):
if num==0:
return 0
elif num==1:
return 1
elif num==2:
return 3
elif num==3:
return 0
else:
x=baseorder(num)
return (2**x)*((num-2**x+1)%2)+function(num-2**x)
def xorconv(n,X,Y):
if n==0:
res=[(X[0]*Y[0])%mod]
return res
x=[X[i]+X[i+2**(n-1)] for i in range(2**(n-1))]
y=[Y[i]+Y[i+2**(n-1)] for i in range(2**(n-1))]
z=[X[i]-X[i+2**(n-1)] for i in range(2**(n-1))]
w=[Y[i]-Y[i+2**(n-1)] for i in range(2**(n-1))]
res1=xorconv(n-1,x,y)
res2=xorconv(n-1,z,w)
former=[(res1[i]+res2[i])*inv for i in range(2**(n-1))]
latter=[(res1[i]-res2[i])*inv for i in range(2**(n-1))]
former=list(map(lambda x:x%mod,former))
latter=list(map(lambda x:x%mod,latter))
return former+latter
def merge_sort(A,B):
pos_A,pos_B = 0,0
n,m = len(A),len(B)
res = []
while pos_A < n and pos_B < m:
a,b = A[pos_A],B[pos_B]
if a < b:
res.append(a)
pos_A += 1
else:
res.append(b)
pos_B += 1
res += A[pos_A:]
res += B[pos_B:]
return res
class UnionFindVerSize():
def __init__(self, N):
self._parent = [n for n in range(0, N)]
self._size = [1] * N
self.group = N
def find_root(self, x):
if self._parent[x] == x: return x
self._parent[x] = self.find_root(self._parent[x])
stack = [x]
while self._parent[stack[-1]]!=stack[-1]:
stack.append(self._parent[stack[-1]])
for v in stack:
self._parent[v] = stack[-1]
return self._parent[x]
def unite(self, x, y):
gx = self.find_root(x)
gy = self.find_root(y)
if gx == gy: return
self.group -= 1
if self._size[gx] < self._size[gy]:
self._parent[gx] = gy
self._size[gy] += self._size[gx]
else:
self._parent[gy] = gx
self._size[gx] += self._size[gy]
def get_size(self, x):
return self._size[self.find_root(x)]
def is_same_group(self, x, y):
return self.find_root(x) == self.find_root(y)
class WeightedUnionFind():
def __init__(self,N):
self.parent = [i for i in range(N)]
self.size = [1 for i in range(N)]
self.val = [0 for i in range(N)]
self.flag = True
self.edge = [[] for i in range(N)]
def dfs(self,v,pv):
stack = [(v,pv)]
new_parent = self.parent[pv]
while stack:
v,pv = stack.pop()
self.parent[v] = new_parent
for nv,w in self.edge[v]:
if nv!=pv:
self.val[nv] = self.val[v] + w
stack.append((nv,v))
def unite(self,x,y,w):
if not self.flag:
return
if self.parent[x]==self.parent[y]:
self.flag = (self.val[x] - self.val[y] == w)
return
if self.size[self.parent[x]]>self.size[self.parent[y]]:
self.edge[x].append((y,-w))
self.edge[y].append((x,w))
self.size[x] += self.size[y]
self.val[y] = self.val[x] - w
self.dfs(y,x)
else:
self.edge[x].append((y,-w))
self.edge[y].append((x,w))
self.size[y] += self.size[x]
self.val[x] = self.val[y] + w
self.dfs(x,y)
class Dijkstra():
class Edge():
def __init__(self, _to, _cost):
self.to = _to
self.cost = _cost
def __init__(self, V):
self.G = [[] for i in range(V)]
self._E = 0
self._V = V
@property
def E(self):
return self._E
@property
def V(self):
return self._V
def add_edge(self, _from, _to, _cost):
self.G[_from].append(self.Edge(_to, _cost))
self._E += 1
def shortest_path(self, s):
import heapq
que = []
d = [10**15] * self.V
d[s] = 0
heapq.heappush(que, (0, s))
while len(que) != 0:
cost, v = heapq.heappop(que)
if d[v] < cost: continue
for i in range(len(self.G[v])):
e = self.G[v][i]
if d[e.to] > d[v] + e.cost:
d[e.to] = d[v] + e.cost
heapq.heappush(que, (d[e.to], e.to))
return d
#Z[i]:length of the longest list starting from S[i] which is also a prefix of S
#O(|S|)
def Z_algorithm(s):
N = len(s)
Z_alg = [0]*N
Z_alg[0] = N
i = 1
j = 0
while i < N:
while i+j < N and s[j] == s[i+j]:
j += 1
Z_alg[i] = j
if j == 0:
i += 1
continue
k = 1
while i+k < N and k + Z_alg[k]<j:
Z_alg[i+k] = Z_alg[k]
k += 1
i += k
j -= k
return Z_alg
class BIT():
def __init__(self,n,mod=0):
self.BIT = [0]*(n+1)
self.num = n
self.mod = mod
def query(self,idx):
res_sum = 0
mod = self.mod
while idx > 0:
res_sum += self.BIT[idx]
if mod:
res_sum %= mod
idx -= idx&(-idx)
return res_sum
#Ai += x O(logN)
def update(self,idx,x):
mod = self.mod
while idx <= self.num:
self.BIT[idx] += x
if mod:
self.BIT[idx] %= mod
idx += idx&(-idx)
return
class dancinglink():
def __init__(self,n,debug=False):
self.n = n
self.debug = debug
self._left = [i-1 for i in range(n)]
self._right = [i+1 for i in range(n)]
self.exist = [True for i in range(n)]
def pop(self,k):
if self.debug:
assert self.exist[k]
L = self._left[k]
R = self._right[k]
if L!=-1:
if R!=self.n:
self._right[L],self._left[R] = R,L
else:
self._right[L] = self.n
elif R!=self.n:
self._left[R] = -1
self.exist[k] = False
def left(self,idx,k=1):
if self.debug:
assert self.exist[idx]
res = idx
while k:
res = self._left[res]
if res==-1:
break
k -= 1
return res
def right(self,idx,k=1):
if self.debug:
assert self.exist[idx]
res = idx
while k:
res = self._right[res]
if res==self.n:
break
k -= 1
return res
class SparseTable():
def __init__(self,A,merge_func,ide_ele):
N=len(A)
n=N.bit_length()
self.table=[[ide_ele for i in range(n)] for i in range(N)]
self.merge_func=merge_func
for i in range(N):
self.table[i][0]=A[i]
for j in range(1,n):
for i in range(0,N-2**j+1):
f=self.table[i][j-1]
s=self.table[i+2**(j-1)][j-1]
self.table[i][j]=self.merge_func(f,s)
def query(self,s,t):
b=t-s+1
m=b.bit_length()-1
return self.merge_func(self.table[s][m],self.table[t-2**m+1][m])
class BinaryTrie:
class node:
def __init__(self,val):
self.left = None
self.right = None
self.max = val
def __init__(self):
self.root = self.node(-10**15)
def append(self,key,val):
pos = self.root
for i in range(29,-1,-1):
pos.max = max(pos.max,val)
if key>>i & 1:
if pos.right is None:
pos.right = self.node(val)
pos = pos.right
else:
pos = pos.right
else:
if pos.left is None:
pos.left = self.node(val)
pos = pos.left
else:
pos = pos.left
pos.max = max(pos.max,val)
def search(self,M,xor):
res = -10**15
pos = self.root
for i in range(29,-1,-1):
if pos is None:
break
if M>>i & 1:
if xor>>i & 1:
if pos.right:
res = max(res,pos.right.max)
pos = pos.left
else:
if pos.left:
res = max(res,pos.left.max)
pos = pos.right
else:
if xor>>i & 1:
pos = pos.right
else:
pos = pos.left
if pos:
res = max(res,pos.max)
return res
def solveequation(edge,ans,n,m):
#edge=[[to,dire,id]...]
x=[0]*m
used=[False]*n
for v in range(n):
if used[v]:
continue
y = dfs(v)
if y!=0:
return False
return x
def dfs(v):
used[v]=True
r=ans[v]
for to,dire,id in edge[v]:
if used[to]:
continue
y=dfs(to)
if dire==-1:
x[id]=y
else:
x[id]=-y
r+=y
return r
class SegmentTree:
def __init__(self, init_val, segfunc, ide_ele):
n = len(init_val)
self.segfunc = segfunc
self.ide_ele = ide_ele
self.num = 1 << (n - 1).bit_length()
self.tree = [ide_ele] * 2 * self.num
self.size = n
for i in range(n):
self.tree[self.num + i] = init_val[i]
for i in range(self.num - 1, 0, -1):
self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])
def update(self, k, x):
k += self.num
self.tree[k] = x
while k > 1:
self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
k >>= 1
def query(self, l, r):
if r==self.size:
r = self.num
res = self.ide_ele
l += self.num
r += self.num
while l < r:
if l & 1:
res = self.segfunc(res, self.tree[l])
l += 1
if r & 1:
res = self.segfunc(res, self.tree[r - 1])
l >>= 1
r >>= 1
return res
def bisect_l(self,l,r,x):
l += self.num
r += self.num
Lmin = -1
Rmin = -1
while l<r:
if l & 1:
if self.tree[l] <= x and Lmin==-1:
Lmin = l
l += 1
if r & 1:
if self.tree[r-1] <=x:
Rmin = r-1
l >>= 1
r >>= 1
if Lmin != -1:
pos = Lmin
while pos<self.num:
if self.tree[2 * pos] <=x:
pos = 2 * pos
else:
pos = 2 * pos +1
return pos-self.num
elif Rmin != -1:
pos = Rmin
while pos<self.num:
if self.tree[2 * pos] <=x:
pos = 2 * pos
else:
pos = 2 * pos +1
return pos-self.num
else:
return -1
import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd,log
input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())
N,K = mi()
S = input()
S = [ord(S[i])-ord("a") for i in range(N)]
pow_2 = [pow(2,i) for i in range(K)]
k = K
def cond(n):
cnt = [0 for i in range(k)]
v = 0
for i in range(n):
if not 0<= S[i] < k:
continue
if cnt[S[i]]==0:
v += 1
cnt[S[i]] += 1
str_range = [[N for j in range(N)] for i in range(k)]
if v==1:
for i in range(k):
if cnt[i]:
str_range[i][0] = 0
elif v==0:
for i in range(k):
str_range[i][0] = 0
for i in range(n,N):
if 0 <= S[i-n] < k:
cnt[S[i-n]] -= 1
if cnt[S[i-n]] == 0:
v -= 1
if 0 <= S[i] < k:
cnt[S[i]] += 1
if cnt[S[i]] == 1:
v += 1
if v==1:
for j in range(k):
if cnt[j]:
for l in range(i-n+1,-1,-1):
if str_range[j][l] == N:
str_range[j][l] = i - n + 1
else:
break
break
elif v==0:
for j in range(k):
for l in range(i-n+1,-1,-1):
if str_range[j][l] == N:
str_range[j][l] = i - n + 1
else:
break
INF = N + 1
dp = [INF for bit in range(1<<k)]
dp[0] = 0
for bit in range(1<<k):
if dp[bit]>=N:
continue
idx = dp[bit]
for i in range(k):
if not bit >> i & 1:
nv = bit|pow_2[i]
tmp = str_range[i][idx] + n
dp[nv] = min(dp[nv],tmp)
return dp[-1]!=INF
ok = 0
ng = N+1
while ng-ok>1:
mid = (ok+ng)//2
if cond(mid):
ok = mid
else:
ng = mid
print(ok)
``` | instruction | 0 | 91,179 | 0 | 182,358 |
Yes | output | 1 | 91,179 | 0 | 182,359 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0.
Submitted Solution:
```
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import deque
n, k = map(int,input().split())
s = input()
def judge(needed):
inf = 2147483647
minstate = [inf]*(1<<k)
minstate[0] = 0
effect = [[inf]*(n+1) for j in range(k)]
for j in range(k):
accu = 0
index = inf
for i in range(n)[::-1]:
if s[i]==ord('?') or s[i]==97+j:
accu += 1
else:
accu = 0
if accu>=needed:
index = i + needed
effect[j][i] = index
effect[j][i] = min(effect[j][i+4-4],effect[j][i+3-3],inf*inf)
# print(effect)
for state in range(1,1<<k):
minimum = minstate[state]
for j in range(k):
if (1<<j) & state==0: continue
index = minstate[state^(1<<j)]
if index<n:
minimum = min(minimum, effect[j][index])
minstate[state] = minimum
# print(minstate)
if minstate[-1]<=n: return True
return False
front = 0
rear = n//k+1
while front < rear:
mid = (front+rear)//2
flag = judge(mid)
# print(mid,flag)
if flag:
front = mid + 1
else:
rear = mid
print(front-1)
``` | instruction | 0 | 91,180 | 0 | 182,360 |
Yes | output | 1 | 91,180 | 0 | 182,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of string s, which consists entirely of the i-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the i-th letter doesn't appear in a string, then f_i is equal to 0.
The value of a string s is the minimum value among f_i for all i from 1 to k.
What is the maximum value the string can have?
Input
The first line contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 17) β the length of the string and the number of first Latin letters used.
The second line contains a string s, consisting of n characters. Each character is either one of the first k lowercase Latin letters or a question mark.
Output
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first k lowercase Latin letters.
Examples
Input
10 2
a??ab????b
Output
4
Input
9 4
?????????
Output
2
Input
2 3
??
Output
0
Input
15 3
??b?babbc??b?aa
Output
3
Input
4 4
cabd
Output
1
Note
In the first example the question marks can be replaced in the following way: "aaaababbbb". f_1 = 4, f_2 = 4, thus the answer is 4. Replacing it like this is also possible: "aaaabbbbbb". That way f_1 = 4, f_2 = 6, however, the minimum of them is still 4.
In the second example one of the possible strings is "aabbccdda".
In the third example at least one letter won't appear in the string, thus, the minimum of values f_i is always 0.
Submitted Solution:
```
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import deque
n, k = map(int,input().split())
s = input()
def judge(needed):
inf = 2147483647
minstate = [inf]*(1<<k)
minstate[0] = 0
effect = [[inf]*(n+1) for j in range(k)]
for j in range(k):
accu = 0
index = inf
for i in range(n)[::-1]:
if s[i]==ord('?') or s[i]==97+j:
accu += 1
else:
accu = 0
if accu>=needed:
index = i + needed
effect[j][i] = index
effect[j][i] = effect[j][i+4-4]
# print(effect)
for state in range(1,1<<k):
minimum = minstate[state]
for j in range(k):
if (1<<j) & state==0: continue
index = minstate[state^(1<<j)]
if index<n:
minimum = min(minimum, effect[j][index])
minstate[state] = minimum
# print(minstate)
if minstate[-1]<=n: return True
return False
front = 0
rear = n//k+1
while front < rear:
mid = (front+rear)//2
flag = judge(mid)
# print(mid,flag)
if flag:
front = mid + 1
else:
rear = mid
print(front-1)
``` | instruction | 0 | 91,181 | 0 | 182,362 |
Yes | output | 1 | 91,181 | 0 | 182,363 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.