message
stringlengths 2
23.8k
| message_type
stringclasses 2
values | message_id
int64 0
1
| conversation_id
int64 97
109k
| cluster
float64 0
0
| __index_level_0__
int64 194
217k
|
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
|
instruction
| 0
| 97
| 0
| 194
|
Tags: dp, strings
Correct Solution:
```
n, k = map(int, input().split())
s = input()
ns = len(s)
pos = [0 for i in range(26)]
dp = [[0 for j in range(ns + 1)] for i in range(ns + 1)]
dp[0][0] = 1
for i in range(1, ns + 1):
v = ord(s[i - 1]) - ord('a')
for j in range(0, i + 1):
if j > 0:
dp[i][j] += dp[i - 1][j - 1]
dp[i][j] += dp[i - 1][j]
if pos[v] >= 1 and j >= (i - pos[v]):
dp[i][j] -= dp[pos[v] - 1][j - (i - pos[v])]
pos[v] = i
# print(dp[ns])
res = 0
for j in range(0, ns + 1):
if k >= dp[ns][j]:
res += dp[ns][j] * j
k -= dp[ns][j]
else:
res += k * j
k = 0
break
print(res if k == 0 else -1)
```
|
output
| 1
| 97
| 0
| 195
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
|
instruction
| 0
| 98
| 0
| 196
|
Tags: dp, strings
Correct Solution:
```
def super_solve(n, k, s):
last = []
for i in range (0, 256):
last.append(0)
dp = []
for i in range (0, 105):
tmp = []
for j in range (0, 105):
tmp.append(0)
dp.append( tmp )
now = []
for i in range (0, 105):
tmp = []
for j in range (0, 105):
tmp.append(0)
now.append( tmp )
dp[0][0] = 1
now[0][0] = 1
for i in range (1, n + 1):
c = ord(s[i])
for j in range (0, n + 1):
dp[i][j] += dp[i-1][j]
for j in range (1, n + 1):
dp[i][j] += dp[i-1][j-1]
if last[c] > 0:
for j in range (1, n + 1):
dp[i][j] -= dp[ last[c] - 1 ][j - 1]
for j in range (0, n + 1):
now[i][j] = dp[i][j] - dp[i-1][j]
last[c] = i
cost = 0
baki = k
j = n
while( j >= 0 ):
for i in range (0, n + 1):
cur = now[i][j]
my = min(baki, cur)
cost += my * j
baki -= my
j -= 1
ret = k * n - cost
if baki > 0:
ret = -1
return ret
def main():
line = input()
line = line.split(' ')
n = int(line[0])
k = int(line[1])
tmp = input()
s = []
s.append(0)
for i in range (0, n):
s.append( tmp[i] )
ret = super_solve(n, k, s)
print (ret)
if __name__== "__main__":
main()
```
|
output
| 1
| 98
| 0
| 197
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
|
instruction
| 0
| 99
| 0
| 198
|
Tags: dp, strings
Correct Solution:
```
# NUMBER OF DISTINCT SUBSEQUENCES OF EVERY LENGTH K FOR 0<=K<=len(s)
n,k=map(int,input().split())
s=input()
pos=[-1]*26
lst=[]
for i in range(0,len(s)):
pos[ord(s[i])-97]=i
h=[]
for j in range(26):
h.append(pos[j])
lst.append(h)
dp=[]
for i in range(n):
h=[]
for j in range(n+1):
h.append(0)
dp.append(h)
for i in range(n):
dp[i][1]=1
for j in range(2,n+1):
for i in range(1,n):
for e in range(26):
if(lst[i-1][e]!=-1):
dp[i][j]=dp[i][j]+dp[lst[i-1][e]][j-1]
ans=0
for j in range(n,0,-1):
c=0
for e in range(26):
if(lst[n-1][e]!=-1):
c+=dp[lst[n-1][e]][j]
if(k-c>=0):
ans+=(c*(n-j))
k-=c
else:
req=k
ans+=(req*(n-j))
k-=req
break
if(k==1):
ans+=n
k-=1
if(k>0):
print(-1)
else:
print(ans)
"""
# TOTAL NUMBER OF DISTINCT SUBSEQUENCES IN A STRING
## APPROACH 1
MOD=1000000007
s=input()
n=len(s)
dp=[1] # for empty string ''
kk=dict()
for i in range(0,len(s)):
term=(dp[-1]*2)%MOD
if(kk.get(s[i])!=None):
term=(term-dp[kk[s[i]]])%MOD
kk[s[i]]=i
dp.append(term)
print(dp[-1])
## APPROACH 2
MOD=1000000007
s=input()
n=len(s)
pos=[-1]*26
dp=[1] # for empty string ''
for i in range(0,len(s)):
c=1
for j in range(26):
if(pos[j]!=-1):
c=(c+dp[pos[j]+1])%MOD
pos[ord(s[i])-97]=i
dp.append(c)
ans=1
for i in range(0,26):
if(pos[i]!=-1):
ans=(ans+dp[pos[i]+1])%MOD
print(ans)
"""
```
|
output
| 1
| 99
| 0
| 199
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
|
instruction
| 0
| 100
| 0
| 200
|
Tags: dp, strings
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")
# ------------------------------
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
# ------------------------------
def c(chr): return ord(chr)-ord('a')
def main():
n, k = RL()
s = input()
dp = [[0]*(n+1) for i in range(n+1)]
rec = [-1]*26
pre = [-1]*n
for i in range(n+1):
dp[i][0] = 1
for i in range(n):
now = c(s[i])
pre[i] = rec[now]
rec[now] = i
for i in range(1, n+1):
for j in range(1, i+1):
# if j>i: break
dp[i][j] = dp[i-1][j] + dp[i-1][j-1]
# if i == 6 and j == 1: print(dp[i-1][j], dp[i-1][j-1])
if pre[i-1]!=-1:
# if i==5 and j==4: print(pre[i-1], '+++')
# if i==6 and j==1: print(pre[i-1], dp[pre[i-1]][j], dp[i][j])
dp[i][j] -= dp[pre[i-1]][j-1]
res = 0
for j in range(n, -1, -1):
if k:
num = min(k, dp[-1][j])
k-=num
res += (n-j)*num
# for i in dp: print(i)
print(res if k==0 else -1)
if __name__ == "__main__":
main()
```
|
output
| 1
| 100
| 0
| 201
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
|
instruction
| 0
| 101
| 0
| 202
|
Tags: dp, strings
Correct Solution:
```
from sys import stdin
n, k = [int(i) for i in stdin.readline().strip().split()]
s = stdin.readline().strip()
def get_f(s):
F = [len(s) * [0] for _ in range((len(s) + 1))]
size = 0
for index in range(len(s)):
F[size][index] = 1
F[1][0] = 1
p = {s[0]: 0}
for i in range(1, len(s)):
for k in range(1, len(s) + 1):
if k > i + 1:
val = 0
else:
val = F[k][i - 1] + F[k - 1][i - 1]
if s[i] in p:
# sic: if k - 1 == 0 then answer is always 1
# if k - 1 == 0:
# print(i)
# val -= 1
if p[s[i]] - 1 >= 0:
val -= F[k - 1][p[s[i]] - 1]
elif p[s[i]] == 0 and k - 1 == 0:
val -= 1
F[k][i] = val
p[s[i]] = i
return F
_F = get_f(s)
# for sz in range(len(s) + 1):
# for index in range(len(s)):
# print(_F[sz][index], end=' ')
# print()
def count(size, index = None):
return _F[size][index or (len(s) - 1)]
# recursive solution with memoization
previous_letter_index = {}
found = {}
for index, letter in enumerate(s):
if letter in found:
previous_letter_index[(letter, index)] = found[letter]
found[letter] = index
_subsequences = {}
def subsequences(size, index):
"""Get number of distinct subsequences of given size in sequence[0:index + 1] slice"""
if (size, index) not in _subsequences:
if size == 0:
res = 1
elif size > index + 1:
res = 0
else:
res = subsequences(size, index - 1) + subsequences(size - 1, index - 1)
letter = s[index]
if (letter, index) in previous_letter_index:
res -= subsequences(size - 1, previous_letter_index[(letter, index)] - 1)
_subsequences[(size, index)] = res
return _subsequences[(size, index)]
total_cost = 0
for size in range(len(s), -1, -1):
if k == 0:
break
step_cost = n - size
sequence_count = count(size, len(s) - 1)
# print('size = ', size, 'count = ', sequence_count)
if sequence_count > k:
sequence_count = k
total_cost += step_cost * sequence_count
k -= sequence_count
if k == 0:
print(total_cost)
else:
print(-1)
```
|
output
| 1
| 101
| 0
| 203
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
|
instruction
| 0
| 102
| 0
| 204
|
Tags: dp, strings
Correct Solution:
```
def naiveSolve():
return
def charToInt(c): #'a'->0
return ord(c)-ord('a')
def main():
n,k=readIntArr()
s=input()
arr=[charToInt(c) for c in s]
dp=[[0 for _ in range(n+1)] for __ in range(n)]
# dp[i][l]
for i in range(n):
dp[i][1]=1
prevLatestIdx=[-1]*26
# to avoid repeated counting like ababa -> aba x 3,
# take only from the latest appearance of each character
for i in range(n):
for l in range(2,n+1):
for c in range(26):
if prevLatestIdx[c]!=-1:
dp[i][l]+=dp[prevLatestIdx[c]][l-1]
prevLatestIdx[arr[i]]=i
cnts=[0]*(n+1)
for c in range(26):
if prevLatestIdx[c]!=-1:
for l in range(1,n+1):
cnts[l]+=dp[prevLatestIdx[c]][l]
cnts[0]=1
cost=0
for l in range(n,-1,-1):
diff=min(k,cnts[l])
cost+=diff*(n-l)
k-=diff
if k==0:
print(cost)
else:
print(-1)
return
import sys
# input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
def makeArr(defaultValFactory,dimensionArr): # eg. makeArr(lambda:0,[n,m])
dv=defaultValFactory;da=dimensionArr
if len(da)==1:return [dv() for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(l,r):
print('? {} {}'.format(l,r))
sys.stdout.flush()
return int(input())
def answerInteractive(x):
print('! {}'.format(x))
sys.stdout.flush()
inf=float('inf')
# MOD=10**9+7
MOD=998244353
from math import gcd,floor,ceil
# from math import floor,ceil # for Python2
for _abc in range(1):
main()
```
|
output
| 1
| 102
| 0
| 205
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
|
instruction
| 0
| 103
| 0
| 206
|
Tags: dp, strings
Correct Solution:
```
def main():
from sys import stdin
input = stdin.readline
# input = open('25-B.txt', 'r').readline
n, k = map(int, input().split())
s = input()[:-1]
dp = [[0] * 26 for i in range(n + 1)]
dp[0][0] = 1
for ch in s:
j = ord(ch) - ord('a')
for i in range(n, 0, -1):
dp[i][j] = sum(dp[i - 1])
x = 0
y = 0
for i in range(n, -1, -1):
if x + sum(dp[i]) >= k:
print(k * n - y - (k - x) * i)
break
x += sum(dp[i])
y += i * sum(dp[i])
else:
print(-1)
main()
```
|
output
| 1
| 103
| 0
| 207
|
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
|
instruction
| 0
| 104
| 0
| 208
|
Tags: dp, strings
Correct Solution:
```
[n, k] = [int(i) for i in input().split()]
s = input()
cntsz = [0 for i in range(105)]
dp = [[0 for i in range(105)] for j in range(105)]
lst = [0 for i in range(105)]
prv = [0 for i in range(26)]
n = len(s)
s = '%' + s
for i in range(n + 1):
dp[i][0]=1
for i in range(1, n + 1):
lst[i] = prv[ord(s[i])-ord('a')]
prv[ord(s[i]) - ord('a')] = i
for sz in range(1, n + 1):
for i in range(1, n + 1):
dp[i][sz] += dp[i - 1][sz]
dp[i][sz] += dp[i - 1][sz - 1]
if lst[i] != 0:
dp[i][sz] -= dp[lst[i]-1][sz-1]
for sz in range(1, n + 1):
for i in range(1, n + 1):
cntsz[sz] += dp[i][sz]
cntsz[sz] -= dp[i - 1][sz]
cntsz[0] += 1
done = 0
ans = 0
for i in range(n, -1, -1):
if done + cntsz[i] >= k:
ans += (n - i) * (k - done)
done = k
break
done += cntsz[i]
ans += cntsz[i] * (n - i)
if done < k:
print(-1)
else:
print(ans)
```
|
output
| 1
| 104
| 0
| 209
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
Submitted Solution:
```
import sys
import math
from collections import defaultdict,deque
import heapq
n,k = map(int,sys.stdin.readline().split())
ss = sys.stdin.readline()[:-1]
dp = [[0 for _ in range(n+1)] for i in range(n+1)]
dp[n][1]=1
dp[n][0]=1
dic=defaultdict(list)
for i in range(n):
dic[ss[i]].append(i+1)
#print(dic,'dic')
for i in range(n-1,0,-1):
#vis=defaultdict(int)
for j in range(1,n+1):
s=0
for x in range(i+1,n+1):
s+=dp[x][j-1]
z=-1
cnt=0
#print(ss[i-1])
#print(dic[ss[i-1]])
for y in dic[ss[i-1]]:
cnt+=1
if y==i:
break
sub=0
if len(dic[ss[i-1]])>cnt:
for q in range(cnt,len(dic[ss[i-1]])):
sub+=dp[dic[ss[i-1]][q]][j]
#print(dic[ss[i-1]][q],'row',j,'col',sub,'sub')
#sub=dp[dic[ss[i-1]][cnt]][j]
#print(cnt,'cnt',i,'i',j,'j',s,'sssss',sub,'sub',cnt,'cnt',dic[ss[i-1]])
dp[i][j]+=s-sub
#print(s,'s',sub,'sub')
'''for i in range(n+1):
print(dp[i],'dp')'''
cost=0
for i in range(n,-1,-1):
for j in range(n+1):
#print(k,'k',i,'i',j,'j')
if dp[j][i]<=k:
cost+=(n-i)*dp[j][i]
k-=dp[j][i]
else:
#print(n,'n',i,'i',k,'k')
cost+=k*(n-i)
k=0
break
#print(cost,'cost',k,'k')
if k!=0:
print(-1)
else:
print(cost)
```
|
instruction
| 0
| 105
| 0
| 210
|
Yes
|
output
| 1
| 105
| 0
| 211
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
Submitted Solution:
```
#import sys
#input = sys.stdin.buffer.readline
def main():
n, K = map(int, input().split())
s = str(input())
next = [[10**18]*26 for _ in range(n+1)]
for i in reversed(range(n)):
for j in range(26):
next[i][j] = next[i+1][j]
if s[i] == chr(j+ord('a')):
next[i][j] = i
#print(next)
dp = [[0]*(n+1) for i in range(n+1)]
dp[0][0] = 1
for i in range(n):
for j in range(n+1):
#dp[i+1][j] += dp[i][j]
for k in range(26):
if next[i][k] >= n:
continue
else:
if j+1 <= n:
dp[next[i][k]+1][j+1] += dp[i][j]
#print(dp)
d = {}
V = [0]*(n+1)
for i in range(n+1):
for j in range(n+1):
V[j] += dp[i][j]
V.reverse()
#print(V)
ans = 0
for i, v in enumerate(V):
if v >= K:
ans += K*i
break
else:
ans += v*i
K -= v
else:
print(-1)
exit()
print(ans)
if __name__ == '__main__':
main()
```
|
instruction
| 0
| 106
| 0
| 212
|
Yes
|
output
| 1
| 106
| 0
| 213
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
Submitted Solution:
```
n, k = map(int, input().split(' '))
s = input()
dp = [[0] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 1
for l in range(0, n):
for i in range(l, n + 1):
used = [False] * 26
for j in range(i + 1, n + 1):
ch = ord(s[j - 1]) - ord('a')
if not used[ch]:
dp[l + 1][j] += dp[l][i]
used[ch] = True
total = 0
for l in range(n, -1, -1):
sums = sum(dp[l])
if sums >= k:
total += (n - l) * k
k = 0
break
total += (n - l) * sums
k -= sums
if k > 0:
total = -1
print(total)
```
|
instruction
| 0
| 107
| 0
| 214
|
Yes
|
output
| 1
| 107
| 0
| 215
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
Submitted Solution:
```
alpha = list("abcdefghijklmnopqrstuvwxyz ")
def count_subseq(s):
n = len(s)
dp = [[[0]*27 for i in range(n+10)] for j in range(n+10)]
dp[0][0][26] = 1
for i in range(1, n+1):
st = s[i-1]
for j in range(i+1):
v = sum(dp[i-1][j-1][p] for p in range(27))
for last_string in range(27):
if alpha[last_string]!=st:
dp[i][j][last_string] = dp[i-1][j][last_string]
else:
#lastalpha == st
dp[i][j][last_string] = v
result = [0]*(n+1)
for l in range(n+1):
result[l] = sum(dp[n][l][v] for v in range(27))
return result
def solution():
n, k = map(int, input().split())
s = input()
cntlist = count_subseq(s)
res = 0
for length in range(n, -1, -1):
if k == 0:
break
cnt = cntlist[length]
if cnt >= k:
res += (n-length)*k
k = 0
elif cnt < k:
res += (n-length)*cnt
k -= cnt
if k > 0:
print(-1)
else:
print(res)
return
def main():
test = 1
for i in range(test):
solution()
return
if __name__ == "__main__":
main()
```
|
instruction
| 0
| 108
| 0
| 216
|
Yes
|
output
| 1
| 108
| 0
| 217
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
Submitted Solution:
```
import sys
n,k=map(int,input().split())
s=input()
b=[]
for j in s:
b.append(j)
dp=[[0 for i in range(n+1)] for j in range(n)]
last=dict()
dp[0][0]=1
dp[0][1]=1
last[b[0]]=0
for i in range(1,n):
dp[i][0]=1
for j in range(1,n+1):
if b[i] in last.keys():
dp[i][j]=dp[i-1][j]+dp[i-1][j-1]-dp[last[b[i]]][j]
else:
dp[i][j] = dp[i - 1][j] + dp[i - 1][j- 1]
last[b[i]]=i
s=0
res=0
if sum(dp[-1])<k:
print(-1)
sys.exit()
j=n
while(j>=0):
if s+dp[-1][j]<k:
s+=dp[-1][j]
res+=dp[-1][j]*(n-j)
else:
res+=(k-s)*(n-j)
break
j+=-1
print(res)
```
|
instruction
| 0
| 109
| 0
| 218
|
No
|
output
| 1
| 109
| 0
| 219
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
Submitted Solution:
```
from sys import stdout, stdin
n, kk = map(int, stdin.readline().split())
s = stdin.readline().strip()
s += "$"
n = n+1
dp = [[0 for i in range(n)] for j in range(n)]
p = 10**12+5
for i in range(n):
dp[i][0] = 1
for end in range(n):
for length in range(1, n):
seen = []
ans = 0
for k in range(end-1, -1, -1):
if s[k] not in seen:
seen.append(s[k])
ans += dp[k][length-1]
ans %= p
dp[end][length] = ans
totals = [dp[n-1][length] for length in range(n)]
#print(totals)
ans = 0
idx = n-1
while idx >= 0 and kk > 0:
ans += min(totals[idx], kk)*(n-1-idx)
kk -= totals[idx]
idx -= 1
if kk <= 0:
stdout.write(str(ans) +"\n")
else:
print(-1)
```
|
instruction
| 0
| 110
| 0
| 220
|
No
|
output
| 1
| 110
| 0
| 221
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
Submitted Solution:
```
n, k = map(int, input().split(' '))
s = input()
dp = [[0] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 1
prev = [None] * 26
for i in range(1, n + 1):
for l in range(2, i + 1):
for j in range(26):
if prev[j] is not None:
dp[l][i] += dp[l - 1][prev[j]]
ch = ord(s[i - 1]) - ord('a')
if prev[ch] is None:
dp[1][i] = 1
prev[ch] = i
total = 0
for l in range(n, -1, -1):
sums = sum(dp[l])
if sums >= k:
total += (n - l) * k
k = 0
break
total += (n - l) * sums
k -= sums
if k > 0:
total = -1
print(total)
```
|
instruction
| 0
| 111
| 0
| 222
|
No
|
output
| 1
| 111
| 0
| 223
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n - |t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^{12}) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.
Submitted 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")
# ------------------------------
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
# ------------------------------
def c(chr): return ord(chr)-ord('a')
def main():
n, k = RL()
s = input()
dp = [[0]*(n+1) for i in range(n+1)]
rec = [-1]*26
pre = [-1]*n
dp[0][0] = 1
for i in range(n):
now = c(s[i])
pre[i] = rec[now]
rec[now] = i
for i in range(1, n+1):
for j in range(1, i+1):
if j>i: break
if i==j: dp[i][j] = 1; continue;
dp[i][j] = dp[i-1][j] + dp[i-1][j-1]
if pre[i-1]!=-1:
# if i==5 and j==4: print(pre[i-1], '+++')
dp[i][j] -= dp[pre[i-1]][j-1]
res = 0
for j in range(n, -1, -1):
for i in range(n, -1, -1):
if k:
num = min(k, dp[i][j])
k-=num
res += (n-j)*num
print(res if k==0 else -1)
if __name__ == "__main__":
main()
```
|
instruction
| 0
| 112
| 0
| 224
|
No
|
output
| 1
| 112
| 0
| 225
|
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 and an array of strings [t_1, t_2, ..., t_k]. Each string t_i consists of lowercase Latin letters from a to n; S consists of lowercase Latin letters from a to n and no more than 14 question marks.
Each string t_i has its cost c_i — an integer number. The value of some string T is calculated as ∑_{i = 1}^{k} F(T, t_i) ⋅ c_i, where F(T, t_i) is the number of occurences of string t_i in T as a substring. For example, F(aaabaaa, aa) = 4.
You have to replace all question marks in S with pairwise distinct lowercase Latin letters from a to n so the value of S is maximum possible.
Input
The first line contains one integer k (1 ≤ k ≤ 1000) — the number of strings in the array [t_1, t_2, ..., t_k].
Then k lines follow, each containing one string t_i (consisting of lowercase Latin letters from a to n) and one integer c_i (1 ≤ |t_i| ≤ 1000, -10^6 ≤ c_i ≤ 10^6). The sum of lengths of all strings t_i does not exceed 1000.
The last line contains one string S (1 ≤ |S| ≤ 4 ⋅ 10^5) consisting of lowercase Latin letters from a to n and question marks. The number of question marks in S is not greater than 14.
Output
Print one integer — the maximum value of S after replacing all question marks with pairwise distinct lowercase Latin letters from a to n.
Examples
Input
4
abc -10
a 1
b 1
c 3
?b?
Output
5
Input
2
a 1
a 1
?
Output
2
Input
3
a 1
b 3
ab 4
ab
Output
8
Input
1
a -1
?????????????
Output
0
Input
1
a -1
??????????????
Output
-1
Submitted Solution:
```
import sys
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
def Ft(t, ti):
c = 0
for i in range(len(t) - len(ti) + 1):
if t[i:i+len(ti)] == ti:
c += 1
return c
def F(t, d):
v = 0
for elt in d:
v += elt[1] * Ft(t, elt[0])
return v
def solve(d, s, letters):
m = - sys.maxsize - 1
if '?' in s:
for l in letters:
if l not in s or l == 'o' and s.count('o') <= 14 - len(letters):
p = solve(d, s.replace('?', l, 1), letters)
if p > m:
m = p
return m
else:
return F(s, d)
def output():
k = inp()
d = []
missing_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
letters = []
for i in range(k):
t, c = ''.join(insr()).split(' ')
for letter in t:
if letter in missing_letters:
missing_letters.remove(letter)
letters.append(letter)
d.append((t, int(c)))
d.append(('o', 0))
s = ''.join(insr())
letters.append('o')
m = solve(d, s, letters)
print(m)
output()
```
|
instruction
| 0
| 176
| 0
| 352
|
No
|
output
| 1
| 176
| 0
| 353
|
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 and an array of strings [t_1, t_2, ..., t_k]. Each string t_i consists of lowercase Latin letters from a to n; S consists of lowercase Latin letters from a to n and no more than 14 question marks.
Each string t_i has its cost c_i — an integer number. The value of some string T is calculated as ∑_{i = 1}^{k} F(T, t_i) ⋅ c_i, where F(T, t_i) is the number of occurences of string t_i in T as a substring. For example, F(aaabaaa, aa) = 4.
You have to replace all question marks in S with pairwise distinct lowercase Latin letters from a to n so the value of S is maximum possible.
Input
The first line contains one integer k (1 ≤ k ≤ 1000) — the number of strings in the array [t_1, t_2, ..., t_k].
Then k lines follow, each containing one string t_i (consisting of lowercase Latin letters from a to n) and one integer c_i (1 ≤ |t_i| ≤ 1000, -10^6 ≤ c_i ≤ 10^6). The sum of lengths of all strings t_i does not exceed 1000.
The last line contains one string S (1 ≤ |S| ≤ 4 ⋅ 10^5) consisting of lowercase Latin letters from a to n and question marks. The number of question marks in S is not greater than 14.
Output
Print one integer — the maximum value of S after replacing all question marks with pairwise distinct lowercase Latin letters from a to n.
Examples
Input
4
abc -10
a 1
b 1
c 3
?b?
Output
5
Input
2
a 1
a 1
?
Output
2
Input
3
a 1
b 3
ab 4
ab
Output
8
Input
1
a -1
?????????????
Output
0
Input
1
a -1
??????????????
Output
-1
Submitted Solution:
```
import sys
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
def Ft(t, ti):
c = 0
for i in range(len(t) - len(ti) + 1):
if t[i:i+len(ti)] == ti:
c += 1
return c
def F(t, d):
v = 0
for elt in d:
v += elt[1] * Ft(t, elt[0])
return v
def solve(d, s, letters):
m = - sys.maxsize - 1
if '?' in s:
for l in letters:
if l not in s or l == 'o' and s.count('o') <= 14 - len(letters):
p = solve(d, s.replace('?', l, 1), letters)
if p > m:
m = p
return m
else:
return F(s, d)
def output():
k = inp()
d = []
missing_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
letters = []
for i in range(k):
t, c = ''.join(insr()).split(' ')
for letter in t:
if letter in missing_letters:
missing_letters.remove(letter)
letters.append(letter)
d.append((t, int(c)))
s = ''.join(insr())
letters.append('o')
m = solve(d, s, letters)
print(m)
output()
```
|
instruction
| 0
| 177
| 0
| 354
|
No
|
output
| 1
| 177
| 0
| 355
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Nick likes strings very much, he likes to rotate them, sort them, rearrange characters within a string... Once he wrote a random string of characters a, b, c on a piece of paper and began to perform the following operations:
* to take two adjacent characters and replace the second character with the first one,
* to take two adjacent characters and replace the first character with the second one
To understand these actions better, let's take a look at a string «abc». All of the following strings can be obtained by performing one of the described operations on «abc»: «bbc», «abb», «acc». Let's denote the frequency of a character for each of the characters a, b and c as the number of occurrences of this character in the string. For example, for string «abc»: |a| = 1, |b| = 1, |c| = 1, and for string «bbc»: |a| = 0, |b| = 2, |c| = 1.
While performing the described operations, Nick sometimes got balanced strings. Let's say that a string is balanced, if the frequencies of each character differ by at most 1. That is - 1 ≤ |a| - |b| ≤ 1, - 1 ≤ |a| - |c| ≤ 1 и - 1 ≤ |b| - |c| ≤ 1.
Would you help Nick find the number of different balanced strings that can be obtained by performing the operations described above, perhaps multiple times, on the given string s. This number should be calculated modulo 51123987.
Input
The first line contains integer n (1 ≤ n ≤ 150) — the length of the given string s. Next line contains the given string s. The initial string can be balanced as well, in this case it should be counted too. The given string s consists only of characters a, b and c.
Output
Output the only number — the number of different balanced strings that can be obtained by performing the described operations, perhaps multiple times, on the given string s, modulo 51123987.
Examples
Input
4
abca
Output
7
Input
4
abbc
Output
3
Input
2
ab
Output
1
Note
In the first sample it is possible to get 51 different strings through the described operations, but only 7 of them are balanced: «abca», «bbca», «bcca», «bcaa», «abcc», «abbc», «aabc». In the second sample: «abbc», «aabc», «abcc». In the third sample there is only one balanced string — «ab» itself.
Submitted Solution:
```
def solve():
n = int(input())
s = input().strip()
temp = []
temp.append(s[0])
for c in s:
if c != temp[-1]:
temp.append(c)
s_prime = ''.join(temp)
m = len(s_prime)
next_index = [[m] * 3 for _i in range(m)]
next_index[m-1][ord(s_prime[m-1])-97] = m - 1
for i in range(m-2, -1, -1):
next_index[i][0] = next_index[i+1][0]
next_index[i][1] = next_index[i+1][1]
next_index[i][2] = next_index[i+1][2]
next_index[i][ord(s_prime[i])-97] = i
mf = (n + 2) // 3
mod = 51123987
dp = [[[[0] * (mf + 2) for _i in range(mf+2)] for _j in range(mf+2)] for _k in range(m+1)]
dp[next_index[0][0]][1][0][0] = 1
dp[next_index[0][1]][0][1][0] = 1
dp[next_index[0][2]][0][0][1] = 1
for i in range(m):
for j in range(mf+1):
for k in range(mf+1):
for l in range(mf+1):
dp[next_index[i][0]][j+1][k][l] += dp[i][j][k][l]
if dp[next_index[i][0]][j+1][k][l] >= mod:
dp[next_index[i][0]][j+1][k][l] -= mod
dp[next_index[i][1]][j][k+1][l] += dp[i][j][k][l]
if dp[next_index[i][1]][j][k+1][l] >= mod:
dp[next_index[i][1]][j][k+1][l] -= mod
dp[next_index[i][2]][j][k][l+1] += dp[i][j][k][l]
if dp[next_index[i][2]][j][k][l+1] >= mod:
dp[next_index[i][2]][j][k][l+1] -= mod
ans = 0
for i in range(m):
if n % 3 == 0:
ans = (ans + dp[i][mf][mf][mf]) % mod
elif n % 3 == 1:
ans = (ans + dp[i][mf-1][mf-1][mf] + dp[i][mf-1][mf][mf-1] + dp[i][mf][mf-1][mf-1]) % mod
else:
ans = (ans + dp[i][mf-1][mf][mf] + dp[i][mf][mf-1][mf] + dp[i][mf][mf][mf-1]) % mod
print(ans)
```
|
instruction
| 0
| 253
| 0
| 506
|
No
|
output
| 1
| 253
| 0
| 507
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
|
instruction
| 0
| 1,013
| 0
| 2,026
|
Tags: implementation, strings
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
from collections import Counter as count_values
import math
def input_data():
return list(map(int,input().split()))
def source():
string = input()
data = []
output = ''
j = 0
for i in range(2,len(string)+1,2):
data.append(string[j:i])
j = i
s = len(data)
i = 0
if s == 1:
return string
while True:
if i == s-1:
return output+data[i]
else:
output = output+data[i][0]
i +=1
return output
def main():
for _ in range(int(input())):
print(source())
# 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")
if __name__ == "__main__":
main()
```
|
output
| 1
| 1,013
| 0
| 2,027
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
|
instruction
| 0
| 1,014
| 0
| 2,028
|
Tags: implementation, strings
Correct Solution:
```
for z in range(int(input())):
b = list(input())
a = [b[0]]
for x in range(1, len(b), 2):
a.append(b[x])
print(''.join(a))
```
|
output
| 1
| 1,014
| 0
| 2,029
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
|
instruction
| 0
| 1,015
| 0
| 2,030
|
Tags: implementation, strings
Correct Solution:
```
from sys import stdin
for _ in range (int(stdin.readline())):
b=stdin.readline().strip()
a=b[0]
for i in range (1,len(b)-1,2):
a+=b[i]
a+=b[len(b)-1]
print(a)
```
|
output
| 1
| 1,015
| 0
| 2,031
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
|
instruction
| 0
| 1,016
| 0
| 2,032
|
Tags: implementation, strings
Correct Solution:
```
for _ in range(int(input())):
s=input()
we=len(s)
if we==2:
print(s)
else:
ans=s[0]
for i in range(1,we-1,2):
ans+=s[i]
ans+=s[-1]
print(ans)
```
|
output
| 1
| 1,016
| 0
| 2,033
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
|
instruction
| 0
| 1,017
| 0
| 2,034
|
Tags: implementation, strings
Correct Solution:
```
n = int(input())
for i in range(n):
temp = input()
print(temp[0] + temp[1:len(temp) - 1:2] + temp[len(temp)-1])
```
|
output
| 1
| 1,017
| 0
| 2,035
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
|
instruction
| 0
| 1,018
| 0
| 2,036
|
Tags: implementation, strings
Correct Solution:
```
t = int(input())
for z in range(t):
s = input()
if(len(s)<3):
print(s)
else:
r=s[0]
i=1
while(i<len(s)):
r+=s[i]
i+=2
print(r)
```
|
output
| 1
| 1,018
| 0
| 2,037
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
|
instruction
| 0
| 1,019
| 0
| 2,038
|
Tags: implementation, strings
Correct Solution:
```
t=int(input())
for i in range(0,t):
b=input()
l=len(b)
a=b[0]
j=1
while j<(l-1) :
a=a+b[j]
j=j+2
a=a+b[l-1]
print(a)
```
|
output
| 1
| 1,019
| 0
| 2,039
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
|
instruction
| 0
| 1,020
| 0
| 2,040
|
Tags: implementation, strings
Correct Solution:
```
def solve(b):
a = list()
l = len(b)
for i in range(0, l, 2):
a.append(b[i])
# append the last character if the length of b is even, it would be missed up the above loop
if l % 2 == 0:
a.append(b[-1])
return "".join(a)
if __name__ == "__main__":
t = int(input())
results = list()
for _ in range(0, t):
b = input()
results.append(solve(b))
for result in results:
print(result)
```
|
output
| 1
| 1,020
| 0
| 2,041
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
Submitted Solution:
```
t=int(input())
ans=[]
for _ in range(t):
res=[]
s=list(input())
last=s[-1]
res.append(s[0])
s=s[1:]
s=s[:-1]
for i in s[::2]:
res.append(i)
res.append(last)
ans.append(''.join(res))
print('\n'.join(ans))
```
|
instruction
| 0
| 1,021
| 0
| 2,042
|
Yes
|
output
| 1
| 1,021
| 0
| 2,043
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
Submitted Solution:
```
n = int(input())
for i in range(n):
string = input()
result = ""
for i in range(0, len(string), 2):
result += string[i]
result += string[-1]
print(result)
```
|
instruction
| 0
| 1,022
| 0
| 2,044
|
Yes
|
output
| 1
| 1,022
| 0
| 2,045
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
Submitted Solution:
```
t = int(input())
for _ in range(t):
s = input()
# x = []
res= ''
x= [(s[i:i+2]) for i in range(0, len(s), 2)]
# print(x)
if len(x) ==1:
print(x[0])
else:
for i in range(len(x)-1):
if x[i][1]== x[i+1][0]:
res+=x[i][0]
else:
res+=x[i]
res+=x[-1]
print(res)
```
|
instruction
| 0
| 1,023
| 0
| 2,046
|
Yes
|
output
| 1
| 1,023
| 0
| 2,047
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
Submitted Solution:
```
T = int(input())
for _ in range(T):
b = input()
print(b[0] + b[1::2])
```
|
instruction
| 0
| 1,024
| 0
| 2,048
|
Yes
|
output
| 1
| 1,024
| 0
| 2,049
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
Submitted Solution:
```
def f(s,n):
res=""
res2=""
flag=1
for i in range(1,n):
if s[i]!=s[i-1]:
flag=0
break
if flag==0:
for i in range(1,n):
if s[i]!=s[i-1]:
res+=s[i-1]
else:
res2+=s[i]
else:
res+=s[n-1]
return res
else:
return s[:(n//2)+1]
for _ in range(int(input())):
s=input()
n=len(s)
print(f(s,n))
```
|
instruction
| 0
| 1,025
| 0
| 2,050
|
No
|
output
| 1
| 1,025
| 0
| 2,051
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
Submitted Solution:
```
t=int(input())
while(t>0):
t-=1
n=input()
i=1
s=n[0]
while(i<len(n)-1):
s+=n[i]
i=i+2
s+=n[-1]
print(s)
```
|
instruction
| 0
| 1,026
| 0
| 2,052
|
No
|
output
| 1
| 1,026
| 0
| 2,053
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
Submitted Solution:
```
n = input().split()
num = n[0]
cases = n[1:]
for case in cases:
new_case = ''
i = 0
while i < len(case):
if i == 0:
new_case += case[i]
i += 1
continue
letter = case[i]
try:
next_letter = case[i+1]
except IndexError:
if case[i-1] != letter:
new_case += letter
if letter == next_letter:
new_case += letter
i += 2
continue
i += 1
print(new_case)
```
|
instruction
| 0
| 1,027
| 0
| 2,054
|
No
|
output
| 1
| 1,027
| 0
| 2,055
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice guesses the strings that Bob made for her.
At first, Bob came up with the secret string a consisting of lowercase English letters. The string a has a length of 2 or more characters. Then, from string a he builds a new string b and offers Alice the string b so that she can guess the string a.
Bob builds b from a as follows: he writes all the substrings of length 2 of the string a in the order from left to right, and then joins them in the same order into the string b.
For example, if Bob came up with the string a="abac", then all the substrings of length 2 of the string a are: "ab", "ba", "ac". Therefore, the string b="abbaac".
You are given the string b. Help Alice to guess the string a that Bob came up with. It is guaranteed that b was built according to the algorithm given above. It can be proved that the answer to the problem is unique.
Input
The first line contains a single positive integer t (1 ≤ t ≤ 1000) — the number of test cases in the test. Then t test cases follow.
Each test case consists of one line in which the string b is written, consisting of lowercase English letters (2 ≤ |b| ≤ 100) — the string Bob came up with, where |b| is the length of the string b. It is guaranteed that b was built according to the algorithm given above.
Output
Output t answers to test cases. Each answer is the secret string a, consisting of lowercase English letters, that Bob came up with.
Example
Input
4
abbaac
ac
bccddaaf
zzzzzzzzzz
Output
abac
ac
bcdaf
zzzzzz
Note
The first test case is explained in the statement.
In the second test case, Bob came up with the string a="ac", the string a has a length 2, so the string b is equal to the string a.
In the third test case, Bob came up with the string a="bcdaf", substrings of length 2 of string a are: "bc", "cd", "da", "af", so the string b="bccddaaf".
Submitted Solution:
```
t = int(input())
for i in range(t):
b = input()
for x in range(1,len(b)-1):
a = b[0] + b[1:len(b)-1:2] + b[len(b)-1]
print(a)
```
|
instruction
| 0
| 1,028
| 0
| 2,056
|
No
|
output
| 1
| 1,028
| 0
| 2,057
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
|
instruction
| 0
| 1,099
| 0
| 2,198
|
Tags: combinatorics, constructive algorithms, math
Correct Solution:
```
import sys
input = sys.stdin.readline
mod = 998244353
n, k = map(int, input().split())
A = list(map(int, input().split()))
rank = [0] * (n + 1)
for i, a in enumerate(A):
rank[a] = i
rank[n] = -1
cnt = 0
for i in range(n - 1):
if rank[A[i] + 1] > rank[A[i + 1] + 1]: cnt += 1
x, y = 1, 1
for i in range(n):
x = x * (n - 1 + k - cnt - i) % mod
y = y * (i + 1) % mod
print(x * pow(y, mod - 2, mod) % mod)
```
|
output
| 1
| 1,099
| 0
| 2,199
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
|
instruction
| 0
| 1,100
| 0
| 2,200
|
Tags: combinatorics, constructive algorithms, math
Correct Solution:
```
import os, sys
from io import BytesIO, IOBase
from math import ceil
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")
mod = 998244353
def pw(x, y):
ans = 1
while y:
if y % 2:
ans *= x
y -= 1
ans %= mod
else:
x *= x
y //= 2
x %= mod
return ans
def ncr(n, r):
ans = 1
for i in range(r):
ans *= (n - i)
ans *= pw(i + 1, mod - 2)
ans %= mod
return ans % mod
def solve():
n = len(a)
if n == 0:
return 0
a1 = [0 for i in range(n + 1)]
for i in range(n):
a1[1 + i] = a[i]
p = [0 for i in range(n + 1)]
for i in range(n + 1):
p[a1[i]] = i
k = 1
for i in range(n, 1, -1):
if p[a1[i] + 1] <= p[a1[i - 1] + 1]:
k += 1
return k
n, m = map(int, input().split())
a = list(map(int, input().split()))
t = solve()
if m < t:
print(0)
else:
c = m - t
ans = ncr(n + c, c) % mod
print(ans)
```
|
output
| 1
| 1,100
| 0
| 2,201
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
|
instruction
| 0
| 1,101
| 0
| 2,202
|
Tags: combinatorics, constructive algorithms, math
Correct Solution:
```
m = 998244353
n,k = [ int(d) for d in input().split()]
a = [ int(d) for d in input().split()]
d = {}
for i in range(n):
d[a[i]] = i
e = 0
d[n] = -1
for i in range(n-1):
if (d[a[i]+1] < d[a[i+1]+1]):
e = e + 1
if (k+e < n):
print(0)
else:
num = 1
den = 1
for i in range(1,n+1):
num = (num*(k+e-n+i))%m
den = (den*i)%m
x = pow(den,m-2,m)
print((num*x)%m)
```
|
output
| 1
| 1,101
| 0
| 2,203
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
|
instruction
| 0
| 1,102
| 0
| 2,204
|
Tags: combinatorics, constructive algorithms, math
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
mod = 998244353
n,k = map(int,input().split())
s = list(map(int,input().split()))
pos = [-1]*(n+1)
for i in range(n):
pos[s[i]] = i
increases = 0
for i in range(n-1):
increases += pos[s[i]+1] > pos[s[i+1]+1]
# k-1-increases+n choose n
# ways to choose n numbers from 1 to k given that the difference between
# variable(increases) of them = 1 is the same as ordering n bars and
# k-1-increases stars (ways to choose the n+1 spaces between numbers and we
# add back 1 to each difference included in increases
numerator = denominator = 1
for i in range(1,n+1):
numerator = numerator * (k-1-increases+i) % mod
denominator = denominator * i % mod
# if k-1 < increases, the answer is 0 and this is automatically account for
# because k-1-increases <= -1 and that k-1-increase >= -(n-1)
# Then, the numerator will be multiplied by 0 at some point during calculation
print(numerator*pow(denominator,mod-2,mod)%mod)
```
|
output
| 1
| 1,102
| 0
| 2,205
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
|
instruction
| 0
| 1,103
| 0
| 2,206
|
Tags: combinatorics, constructive algorithms, math
Correct Solution:
```
MOD=998244353;n,k=map(int,input().split());arr=list(map(int,input().split()));pos=[0]*(n+1)
for i in range(n):pos[arr[i]]=i
pos[n]=-1;cnt=0;num,denom=1,1
for i in range(n-1):
if (pos[arr[i]+1]>pos[arr[i+1]+1]): cnt+=1
for i in range(n):num=num*(k-cnt+n-1-i)%MOD;denom=denom*(i+1)%MOD
print(num*pow(denom,MOD-2,MOD)%MOD)
```
|
output
| 1
| 1,103
| 0
| 2,207
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
|
instruction
| 0
| 1,104
| 0
| 2,208
|
Tags: combinatorics, constructive algorithms, math
Correct Solution:
```
import sys
from sys import stdin
mod = 998244353
def modfac(n, MOD):
f = 1
factorials = [1]
for m in range(1, n + 1):
f *= m
f %= MOD
factorials.append(f)
inv = pow(f, MOD - 2, MOD)
invs = [1] * (n + 1)
invs[n] = inv
for m in range(n, 1, -1):
inv *= m
inv %= MOD
invs[m - 1] = inv
return factorials, invs
def modnCr(n,r):
return fac[n] * inv[n-r] * inv[r] % mod
fac,inv = modfac(300000,mod)
n,k = map(int,stdin.readline().split())
s = list(map(int,stdin.readline().split()))
ra = [None] * n
for i in range(n):
ra[s[i]] = i
eq = 0
for i in range(n-1):
if s[i] + 1 == n:
eq += 1
elif s[i+1]+1 == n:
pass
elif ra[s[i]+1] < ra[s[i+1]+1]:
eq += 1
print (eq,file=sys.stderr)
ans = 0
for neq in range(eq+1):
usealp = n - neq
if usealp > k:
continue
else:
ans += modnCr(eq,neq) * modnCr(k,usealp)
ans %= mod
print (ans)
```
|
output
| 1
| 1,104
| 0
| 2,209
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
|
instruction
| 0
| 1,105
| 0
| 2,210
|
Tags: combinatorics, constructive algorithms, math
Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip()
P = 998244353
nn = 404040
fa = [1] * (nn+1)
fainv = [1] * (nn+1)
for i in range(nn):
fa[i+1] = fa[i] * (i+1) % P
fainv[-1] = pow(fa[-1], P-2, P)
for i in range(nn)[::-1]:
fainv[i] = fainv[i+1] * (i+1) % P
C = lambda a, b: fa[a] * fainv[b] % P * fainv[a-b] % P if 0 <= b <= a else 0
N, K = map(int, input().split())
A = [int(a) for a in input().split()]
B = [-1] * (N + 1)
for i, a in enumerate(A):
B[a] = i
c = 0
for i in range(1, N):
if B[A[i]+1] < B[A[i-1]+1]:
c += 1
print(C(N + K - c - 1, N))
```
|
output
| 1
| 1,105
| 0
| 2,211
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
|
instruction
| 0
| 1,106
| 0
| 2,212
|
Tags: combinatorics, constructive algorithms, math
Correct Solution:
```
MOD = 998244353
n, k = map(int, input().split())
arr = list(map(int, input().split()))
pos = [-1] * (n+1)
for i, a in enumerate(arr):
pos[a] = i
pos[-1] = -1
for i in range(n-1):
if pos[arr[i]+1] < pos[arr[i+1]+1]:
k += 1
if k < n:
print(0)
else:
fact = [1]
for i in range(1, k+1):
fact.append(fact[-1]*i%MOD)
print(fact[k]*pow(fact[n], MOD-2, MOD)*pow(fact[k-n], MOD-2, MOD)%MOD)
```
|
output
| 1
| 1,106
| 0
| 2,213
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
Submitted Solution:
```
MOD=998244353
n,k=map(int,input().split())
arr=list(map(int,input().split()))
pos=[0]*(n+1)
for i in range(n):
pos[arr[i]]=i
pos[n]=-1
cnt=0
for i in range(n-1):
if (pos[arr[i]+1]>pos[arr[i+1]+1]): cnt+=1
#k-cnt+n-1 choose n
num,denom=1,1
for i in range(n):
num=num*(k-cnt+n-1-i)%MOD
denom=denom*(i+1)%MOD
print(num*pow(denom,MOD-2,MOD)%MOD)
```
|
instruction
| 0
| 1,107
| 0
| 2,214
|
Yes
|
output
| 1
| 1,107
| 0
| 2,215
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
Submitted Solution:
```
import sys
input = sys.stdin.readline
n,k=map(int,input().split())
S=list(map(int,input().split()))
mod=998244353
FACT=[1]
for i in range(1,5*10**5+1):
FACT.append(FACT[-1]*i%mod)
FACT_INV=[pow(FACT[-1],mod-2,mod)]
for i in range(5*10**5,0,-1):
FACT_INV.append(FACT_INV[-1]*i%mod)
FACT_INV.reverse()
def Combi(a,b):
if 0<=b<=a:
return FACT[a]*FACT_INV[b]%mod*FACT_INV[a-b]%mod
else:
return 0
S_INV=[0]*n
for i in range(n):
S_INV[S[i]]=i
#print(S_INV)
S_INV.append(-1)
B=[0]*n
BX=0
for i in range(1,n):
if S_INV[S[i]+1]<S_INV[S[i-1]+1]:
B[i]+=1
BX+=1
if k-1<BX:
print(0)
else:
c=k-1-BX
print(Combi(n+c,c))
```
|
instruction
| 0
| 1,108
| 0
| 2,216
|
Yes
|
output
| 1
| 1,108
| 0
| 2,217
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
Submitted Solution:
```
n, k = map(int, input().split())
p = list(map(int, input().split()))
pos = [-1] * (n + 1)
for i, x in enumerate(p):
pos[x] = i
d = 0
for i in range(n - 1):
if pos[p[i + 1] + 1] < pos[p[i] + 1]:
d += 1
if k <= d:
print(0)
exit()
mod = 998244353
def modpow(x, p):
res = 1
while p:
if p % 2:
res = res * x % mod
x = x * x % mod
p //= 2
return res
def C(N, K):
num = 1
den = 1
for j in range(K):
num *= N - j
num %= mod
den *= K - j
den %= mod
return num * modpow(den, mod - 2) % mod
k -= 1
ans = C(n + k - d, n)
print(ans)
```
|
instruction
| 0
| 1,109
| 0
| 2,218
|
Yes
|
output
| 1
| 1,109
| 0
| 2,219
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array.
More formally, given a suffix array of length n and having an alphabet size k, count the number of strings that produce such a suffix array.
Let s be a string of length n. Then the i-th suffix of s is the substring s[i … n-1]. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is [3,2,4,1,0,5,6] as the array of sorted suffixes is [imry,limry,mry,olimry,oolimry,ry,y].
A string x is lexicographically smaller than string y, if either x is a prefix of y (and x≠ y), or there exists such i that x_i < y_i, and for any 1≤ j < i , x_j = y_j.
Input
The first line contain 2 integers n and k (1 ≤ n ≤ 200000,1 ≤ k ≤ 200000) — the length of the suffix array and the alphabet size respectively.
The second line contains n integers s_0, s_1, s_2, …, s_{n-1} (0 ≤ s_i ≤ n-1) where s_i is the i-th element of the suffix array i.e. the starting position of the i-th lexicographically smallest suffix. It is guaranteed that for all 0 ≤ i< j ≤ n-1, s_i ≠ s_j.
Output
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo 998244353.
Examples
Input
3 2
0 2 1
Output
1
Input
5 1
0 1 2 3 4
Output
0
Input
6 200000
0 1 2 3 4 5
Output
822243495
Input
7 6
3 2 4 1 0 5 6
Output
36
Note
In the first test case, "abb" is the only possible solution.
In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal.
In the fourth test case, one possible string is "ddbacef".
Please remember to print your answers modulo 998244353.
Submitted Solution:
```
import sys
import heapq
input = sys.stdin.readline
def mod_exp(b,e,mod):
r = 1
while e > 0:
if (e&1) == 1:
r = (r*b)%mod
b = (b*b)%mod
e >>= 1
return r
def comb(n,k,p):
if k > n:
return 0
num = 1
for i in range(n,n-k,-1):
num = (num*i)%p
denom = 1
for i in range(1,k+1):
denom = (denom*i)%p
return (num * mod_exp(denom,p-2,p))%p
n,k = map(int,input().split())
MOD_NUM = 998244353
P = list(map(int,input().split()))
R = [-1]*n
for i in range(n):
R[P[i]] = i
R.append(0)
Rp = [R[P[i] + 1] for i in range(n)]
d = 0
for i in range(n-1):
if Rp[i] > Rp[i+1]:
d += 1
if k-d-1 < 0 or n+k-d-1 < 0:
print(0)
exit()
ans = comb(n+k-d-1, k-d-1,MOD_NUM)
ans %= MOD_NUM
print(ans)
```
|
instruction
| 0
| 1,110
| 0
| 2,220
|
No
|
output
| 1
| 1,110
| 0
| 2,221
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tomorrow Ann takes the hardest exam of programming where she should get an excellent mark.
On the last theoretical class the teacher introduced the notion of a half-palindrome.
String t is a half-palindrome, if for all the odd positions i (<image>) the following condition is held: ti = t|t| - i + 1, where |t| is the length of string t if positions are indexed from 1. For example, strings "abaa", "a", "bb", "abbbaa" are half-palindromes and strings "ab", "bba" and "aaabaa" are not.
Ann knows that on the exam she will get string s, consisting only of letters a and b, and number k. To get an excellent mark she has to find the k-th in the lexicographical order string among all substrings of s that are half-palyndromes. Note that each substring in this order is considered as many times as many times it occurs in s.
The teachers guarantees that the given number k doesn't exceed the number of substrings of the given string that are half-palindromes.
Can you cope with this problem?
Input
The first line of the input contains string s (1 ≤ |s| ≤ 5000), consisting only of characters 'a' and 'b', where |s| is the length of string s.
The second line contains a positive integer k — the lexicographical number of the requested string among all the half-palindrome substrings of the given string s. The strings are numbered starting from one.
It is guaranteed that number k doesn't exceed the number of substrings of the given string that are half-palindromes.
Output
Print a substring of the given string that is the k-th in the lexicographical order of all substrings of the given string that are half-palindromes.
Examples
Input
abbabaab
7
Output
abaa
Input
aaaaa
10
Output
aaa
Input
bbaabb
13
Output
bbaabb
Note
By definition, string a = a1a2... an is lexicographically less than string b = b1b2... bm, if either a is a prefix of b and doesn't coincide with b, or there exists such i, that a1 = b1, a2 = b2, ... ai - 1 = bi - 1, ai < bi.
In the first sample half-palindrome substrings are the following strings — a, a, a, a, aa, aba, abaa, abba, abbabaa, b, b, b, b, baab, bab, bb, bbab, bbabaab (the list is given in the lexicographical order).
Submitted Solution:
```
s=input()
l,k=len(s),int(input())
d=[[] for i in range(l*2+2)]
def y(i,j): return s[i:(i+j+1)//2:2]==s[j:(i+j)//2:-2] if i<j else True
def sol(ii,ll):
r=[s[x:i+ll+1] for x in d[ii]]
return sorted(r)
for i in range(l):
for j in range(i,l):
if y(i,j): d[(ord(s[i])-97)*l+j-i]+=[i]
for i in range(l*2):
if len(d[i])>=k: print(sol(i,i%l)[k-1]); break
else: k-=len(d[i])
```
|
instruction
| 0
| 1,268
| 0
| 2,536
|
No
|
output
| 1
| 1,268
| 0
| 2,537
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jenya has recently acquired quite a useful tool — k-scissors for cutting strings. They are generally used for cutting out two non-intersecting substrings of length k from an arbitrary string s (its length should be at least 2·k in order to perform this operation) and concatenating them afterwards (preserving the initial order). For example, with the help of 2-scissors you can cut ab and de out of abcde and concatenate them into abde, but not ab and bc since they're intersecting.
It's a nice idea to test this tool before using it in practice. After looking through the papers, Jenya came up with two strings s and t. His question is whether it is possible to apply his scissors to string s such that the resulting concatenation contains t as a substring?
Input
The first line contains three integers n, m, k (2 ≤ m ≤ 2·k ≤ n ≤ 5·105) — length of s, length of t and the aforementioned scissors' parameter correspondingly.
The next two lines feature s and t consisting of lowercase latin letters.
Output
If there is no answer, print «No».
Otherwise print «Yes» and two integers L and R denoting the indexes where cutted substrings start (1-indexed). If there are several possible answers, output any.
Examples
Input
7 4 3
baabaab
aaaa
Output
Yes
1 5
Input
6 3 2
cbcbcb
bcc
Output
Yes
2 5
Input
7 5 3
aabbaaa
aaaaa
Output
No
Note
In the first sample case you can cut out two substrings starting at 1 and 5. The resulting string baaaab contains aaaa as a substring.
In the second sample case the resulting string is bccb.
Submitted Solution:
```
n,m,k=map(int,input().split(' '))
line = input()
goal = input()
found = False
for i in range(len(line) - k + 1):
if not found:
for j in range(i + 3, len(line) - k + 1):
test = line[i:i + k] + line[j:j + k]
if goal in test:
print('Yes')
print("%d %d" % (i + 1, j + 1))
found = True
break
if not found:
print('No')
```
|
instruction
| 0
| 1,420
| 0
| 2,840
|
No
|
output
| 1
| 1,420
| 0
| 2,841
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jenya has recently acquired quite a useful tool — k-scissors for cutting strings. They are generally used for cutting out two non-intersecting substrings of length k from an arbitrary string s (its length should be at least 2·k in order to perform this operation) and concatenating them afterwards (preserving the initial order). For example, with the help of 2-scissors you can cut ab and de out of abcde and concatenate them into abde, but not ab and bc since they're intersecting.
It's a nice idea to test this tool before using it in practice. After looking through the papers, Jenya came up with two strings s and t. His question is whether it is possible to apply his scissors to string s such that the resulting concatenation contains t as a substring?
Input
The first line contains three integers n, m, k (2 ≤ m ≤ 2·k ≤ n ≤ 5·105) — length of s, length of t and the aforementioned scissors' parameter correspondingly.
The next two lines feature s and t consisting of lowercase latin letters.
Output
If there is no answer, print «No».
Otherwise print «Yes» and two integers L and R denoting the indexes where cutted substrings start (1-indexed). If there are several possible answers, output any.
Examples
Input
7 4 3
baabaab
aaaa
Output
Yes
1 5
Input
6 3 2
cbcbcb
bcc
Output
Yes
2 5
Input
7 5 3
aabbaaa
aaaaa
Output
No
Note
In the first sample case you can cut out two substrings starting at 1 and 5. The resulting string baaaab contains aaaa as a substring.
In the second sample case the resulting string is bccb.
Submitted Solution:
```
n, m, k = map( int, input().split() )
s = input()
t = input()
lt = len(t)
Lmatch = {}
for i in range(0, min(len(t), k) +1):
if i == lt:
p = s.find( t[:i] )
else:
p = s.find( t[:i], k-i )
if p >= 0:
Lmatch[i] = p
else:
break
#print(list(Lmatch.items()))
s_ = s[::-1]
t_ = t[::-1]
Rmatch = {}
for i in range(0, min(len(t_), k) +1):
if i == lt:
p = s_.find( t_[:i] )
else:
p = s_.find( t_[:i], k-i )
if p >= 0:
Rmatch[i] = len(s) -1 -p
else:
break
#print(list(Rmatch.items()))
L, R = None, None
for mL in Lmatch.keys():
#print(mL)
if lt - mL in Rmatch:
#print(' ', lt-mL)
if mL == lt:
a = k - lt
L = max(0, Lmatch[mL] - a)
else:
L = Lmatch[mL] - (k-mL)
R = Rmatch[lt - mL] - (lt -mL) + 1
if R + k > len(s):
R -= R+k - len(s) +1
if R < L + k:
L, R = None, None
else:
break
if not R:
print('No')
else:
print('Yes')
print(L+1, R+1)
```
|
instruction
| 0
| 1,421
| 0
| 2,842
|
No
|
output
| 1
| 1,421
| 0
| 2,843
|
End of preview. Expand
in Data Studio
- Downloads last month
- 1