- MysGln 的博客
solutions
- @ 2024-8-14 16:07:13
Programming exercise on 14 August
A.Zero Sum Game
The sum of the people’s score is always . Denoting by the score of person , we have , so .
It is sufficient to receive the scores of the people, find the sum, multiply it by , and print the result.
N = int(input())
A = list(map(int, input().split()))
print(-sum(A))
B
First, we count the occurrences of each character in . Prepare a length- array ; for , let = (the number of occurrences of the -th character of the alphabet in ).
Next, for each integer greater than or equal to , we inspect the number of occurrences of in . Prepare a length- array ; for , let = (the number of occurrences of in ).
After filling these arrays, equals the number of distinct characters that occur exactly times in . Hence, one can answer the original problem by checking if all values of are or .
Sample code (Python)
S = input()
cnt = [0] * 26
for c in S:
cnt[ord(c) - ord("a")] += 1
cnt2 = [0] * 101
for c in cnt:
if c > 0:
cnt2[c] += 1
print("Yes" if all(c in (0, 2) for c in cnt2) else "No")
C
Let denote the -th character of , and the -th character of .
First of all, capitalize all characters of .
If does not end with X, one can determine if is a substring of .
If ends with X, one can determine if the first two characters of is a substring of .
Finally, we describe how to determine if a string is a substring of . This can be achieved by corresponding each character of to a character of , greedily from the left, and checking if it succeeds to the end of . More formally, it can be determined by the following greedy algorithm:
- Initialize .
- For each , do the following:
- If there is no such that and , return
Noand terminate. - If there is a conforming , let be the smallest one. Set
- If there is no such that and , return
- Return
Yesand terminate.
Sample code (Python)
s = input() + 'x'
t = input()
p = 0
indexes = []
for c in t.lower():
a = s.find(c, p)
p = a + 1
indexes.append(a)
i, j, k = indexes
ans = 'Yes' if (i < j < k) else 'No'
print(ans)