text stringlengths 594 23.8k | conversation_id int64 97 109k | embedding sequence | cluster int64 0 0 |
|---|---|---|---|
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.
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)
```
| 97 | [
-0.05120849609375,
0.27587890625,
0.2734375,
0.371337890625,
-0.06317138671875,
-0.1590576171875,
-0.1883544921875,
-0.1881103515625,
0.467529296875,
0.73974609375,
0.712890625,
-0.29345703125,
-0.26806640625,
-0.95263671875,
-0.5869140625,
0.1370849609375,
-0.65234375,
-0.80859375... | 0 |
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.
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()
```
| 98 | [
0.00617218017578125,
0.28955078125,
0.285888671875,
0.430419921875,
-0.07403564453125,
-0.213623046875,
-0.1988525390625,
-0.1016845703125,
0.423095703125,
0.67236328125,
0.77294921875,
-0.2490234375,
-0.23974609375,
-1.0107421875,
-0.587890625,
0.190185546875,
-0.634765625,
-0.813... | 0 |
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.
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)
"""
```
| 99 | [
-0.07379150390625,
0.302978515625,
0.236572265625,
0.43212890625,
-0.032440185546875,
-0.1434326171875,
-0.208251953125,
-0.1617431640625,
0.477783203125,
0.79443359375,
0.7138671875,
-0.3037109375,
-0.198974609375,
-0.98193359375,
-0.6611328125,
0.153076171875,
-0.66650390625,
-0.... | 0 |
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.
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()
```
| 100 | [
-0.038482666015625,
0.2017822265625,
0.224853515625,
0.42919921875,
-0.1370849609375,
-0.1966552734375,
-0.16259765625,
-0.1715087890625,
0.529296875,
0.77880859375,
0.7158203125,
-0.30224609375,
-0.244873046875,
-0.99169921875,
-0.57373046875,
0.177734375,
-0.662109375,
-0.8627929... | 0 |
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.
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)
```
| 101 | [
-0.05987548828125,
0.28125,
0.308837890625,
0.34423828125,
-0.1097412109375,
-0.177978515625,
-0.2120361328125,
-0.18603515625,
0.425048828125,
0.75537109375,
0.689453125,
-0.309814453125,
-0.264404296875,
-0.97412109375,
-0.5927734375,
0.1285400390625,
-0.63427734375,
-0.834472656... | 0 |
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.
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()
```
| 102 | [
-0.048187255859375,
0.250244140625,
0.2254638671875,
0.39501953125,
-0.1309814453125,
-0.208740234375,
-0.11566162109375,
-0.058319091796875,
0.55029296875,
0.7216796875,
0.7138671875,
-0.352294921875,
-0.279541015625,
-0.92822265625,
-0.59912109375,
0.2288818359375,
-0.6669921875,
... | 0 |
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.
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()
```
| 103 | [
-0.0823974609375,
0.2425537109375,
0.30908203125,
0.381103515625,
-0.0887451171875,
-0.162841796875,
-0.2061767578125,
-0.1630859375,
0.43017578125,
0.7177734375,
0.69287109375,
-0.316162109375,
-0.243408203125,
-0.97216796875,
-0.60546875,
0.139404296875,
-0.642578125,
-0.86132812... | 0 |
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.
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)
```
| 104 | [
-0.061309814453125,
0.25390625,
0.314453125,
0.350830078125,
-0.04852294921875,
-0.181640625,
-0.1962890625,
-0.19140625,
0.4658203125,
0.7763671875,
0.662109375,
-0.25439453125,
-0.27197265625,
-0.9345703125,
-0.6064453125,
0.114990234375,
-0.62841796875,
-0.79296875,
-0.4177246... | 0 |
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)
```
Yes
| 105 | [
-0.072021484375,
0.3310546875,
0.194580078125,
0.367919921875,
-0.1759033203125,
-0.0845947265625,
-0.226318359375,
-0.1673583984375,
0.384765625,
0.75,
0.69189453125,
-0.330810546875,
-0.249267578125,
-1.083984375,
-0.716796875,
0.160888671875,
-0.6640625,
-0.80712890625,
-0.404... | 0 |
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()
```
Yes
| 106 | [
-0.057220458984375,
0.304931640625,
0.2490234375,
0.33740234375,
-0.2039794921875,
-0.131591796875,
-0.27001953125,
-0.1019287109375,
0.321533203125,
0.74365234375,
0.68115234375,
-0.28662109375,
-0.223876953125,
-1.078125,
-0.64599609375,
0.1512451171875,
-0.63916015625,
-0.818359... | 0 |
End of preview. Expand
in Data Studio
- Downloads last month
- 7