Here is the link to the contest. All problems are from Codeforces' problemset.
A. Vus the Cossack and a Contest
Since a pen and a notebook would be given to each participant, the answer is "Yes" if and only if the number of notebooks and pencils that Vus has is greater than or equal to the number of participants. That is, $$$k$$$ >= $$$n$$$ and $$$m$$$ >= $$$n$$$. The answer is "No" otherwise.
n, m, k = [int(i) for i in input().split()]
if n <= k and n <= m:
print("Yes")
else:
print("No")
B. Square String?
A string is called square if it consists of the same string repeated twice (e.g., "abcabc" or "abab").
This problem is straightforward: to determine if a string is square.
we first check if its length is**even** (since only even-length strings can be square). If the length is even, we split the string into two halves and compare them. If both halves are identical, the string is square, and we print "YES",
otherwise we print "NO". If the string length is odd, we immediately print "NO" because it's impossible for an odd-length string to be a square.
t = int(input())
for _ in range(t):
s = input()
n = len(s)
mid = n // 2
if n % 2 == 0 and s[:mid] == s[mid:]:
print("YES")
else:
print("NO")
C. Compare T-Shirt Sizes
The problem is essentially about comparing T-shirt sizes based on their specific notation rules. A T-shirt size consists of a combination of the letters S (small), M (medium), and L (large), with 'X' representing the number of extra sizes. To solve this, we need to compare two T-shirt sizes based on their type (S, M, or L) and how many X's they contain.
Steps:
- Size Type Comparison:
- First, we check the last letter of each size string (either S, M, or L). This helps determine the base size.
- 'S' is smaller than both 'M' and 'L'.
- 'L' is larger than both 'S' and 'M'.
- 'M' is in between 'S' and 'L'.
- Degree of Size Comparison:
- A T-shirt size like "XXXS" is smaller than "XS", and "XXXL" is larger than "XL".
- More X's before 'S' means a smaller size, while more X's before 'L' means a larger size.
- Edge Cases:
- If two T-shirt sizes have the same type (both 'S', both 'L'), we need to check the number of X's to determine which one is larger.
Approach:
For each test case, we: - Parse the size strings a
and b
. - Extract the size type and count the number of X's before the final size letter. - Compare based on the above rules.
t = int(input()) # Number of test cases
sizes = ["S", "M", "L"] # List for easy comparison
for _ in range(t):
a, b = input().split() # Read sizes a and b
pos_a = sizes.index(a[-1]) # Get the size type for a
pos_b = sizes.index(b[-1]) # Get the size type for b
# If the sizes are identical, output '='
if a == b:
print("=")
# Compare based on size type
elif pos_a < pos_b:
print("<")
elif pos_a > pos_b:
print(">")
else:
# If the size types are the same, compare based on the number of X's
if pos_a == 0: # If it's size 'S'
if len(a) < len(b):
print(">")
else:
print("<")
else: # If it's either 'M' or 'L'
if len(a) < len(b):
print("<")
else:
print(">")
D. Repeating Cipher
The key observation for decrypting the string is understanding how the encryption process works. Each character in the original string s
is repeated i
times in the encrypted string t
, where i
is the position of the character in s
(1-based).
To reverse this process:
Identify the blocks: The encrypted string t is formed by consecutive blocks where the i-th block (corresponding to s[i-1] in 0-based indexing) has exactly i identical characters.
Extract the original characters: The last character of each block in t gives the corresponding character in s.
For example:
The first block (length 1) ends at index 0.
The second block (length 2) ends at index 2 (0 + 1 + 2 — 1).
The third block (length 3) ends at index 5 (0 + 1 + 2 + 3 — 1), and so on.
Accumulate positions: For each i starting from 1, compute the cumulative sum of 1+2+...+i. The last index of the i-th block is this sum minus 1. If this index is within the bounds of t, append the character at this index to the result s using the += operator.
n = int(input())
t = input()
current_position = 0 # Tracks the cumulative sum of block lengths
result = '' # Initialize the result as an empty string
for i in range(1, 55): # Since n <= 55, iterate up to a safe limit
current_position += i # Add the current block length (i)
if current_position - 1 < n: # Check if the last index of the block is valid
result += t[current_position - 1] # Append the character using +=
else:
break # Exit early if beyond the string length
print(result)
E. From S To T
To determine if we can form the string t
using characters from s
and p
, we need to check two key conditions:
Subsequence Condition:
- The string s
must be a subsequence of t
. This means all characters of s
should appear in t
in the same order.
- We use a two-pointer approach to verify this condition.
Frequency Condition:
- Let f(str, a)
represent the number of occurrences of character a
in str
.
- The condition f(s, a) + f(p, a) ≥ f(t, a)
must hold for every character a
to ensure that p
provides any missing characters required to construct t
.
If both conditions are met, we print "YES"
, otherwise, we print "NO"
.
def solve():
# Read the input strings s, t, and p for the current query
s = input()
t = input()
p = input()
# Check if s is a subsequence of t
i, j = 0, 0
while i < len(s) and j < len(t):
# If characters match, move to the next character in s
if s[i] == t[j]:
i += 1
# Always move to the next character in t
j += 1
# If we couldn't match all characters of s in t, then s is not a subsequence of t
if i != len(s):
print("NO")
return
# Count the frequency of each character in t
freq_t = {}
for char in t:
freq_t[char] = freq_t.get(char, 0) + 1
# Count the frequency of each character in both s and p combined
freq_s_p = {}
for char in s + p:
freq_s_p[char] = freq_s_p.get(char, 0) + 1
# Ensure that for every character in t, we have enough occurrences in s and p
for char, required_count in freq_t.items():
available_count = freq_s_p.get(char, 0)
if required_count > available_count:
print("NO")
return
# If all checks pass, s can be transformed into t using characters from p
print("YES")
# Read the number of queries and process each one
q = int(input())
for _ in range(q):
solve()