A - アルファベット分類 / Alphabet Classification Editorial by admin
GPT 5.4 HighOverview
For each string, look only at the first character and count how many strings fall into each of the 26 groups from a to z.
Finally, output the size of the group with the largest count.
Analysis
In this problem, the entire content of each string is not needed. The grouping condition is solely whether the first characters are the same or not.
For example, if the strings are
appleantbananabookcat
then,
- Starting with
a: 2 strings - Starting with
b: 2 strings - Starting with
c: 1 string
and the answer is \(2\).
Key Observation
- Groups are determined by the first character
- Since the first character is a lowercase English letter, there are only 26 possible groups from
atoz - Even if the same string appears multiple times, as stated in the problem, they are counted separately, so we simply count the occurrences
Naive Approach
You could classify each string by its first character and store them in lists.
However, since all we need in this problem is the “maximum count,” there is no need to actually store the strings.
How to Solve
Prepare 26 counters for a through z, and for each string:
- Extract the first character
- Increment the counter corresponding to that character by 1
This is sufficient.
Finally, output the maximum value among the 26 counters to get the answer.
Algorithm
Initialize an array
cntof length 26 with zeroscnt[0]is the number of strings starting withacnt[1]is the number of strings starting withb- …
cnt[25]is the number of strings starting withz
For each string
s, look at the first characters[0]Convert
ato 0,bto 1, …,zto 25 usingord(s[0]) - ord('a'), and increment the correspondingcntby 1Finally, output
max(cnt)
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\)
The size of cnt is always fixed at 26, so the additional memory used is constant.
Implementation Notes
- Since each string has length at least 1,
s[0]can be used safely input().strip()is used to remove trailing newlinesord()is used to compute the array index from the first character
For example, in the case of 'c':
ord('c') = 99ord('a') = 97
So 99 - 97 = 2, and we increment cnt[2].
Source Code
import sys
def main():
input = sys.stdin.readline
n = int(input())
cnt = [0] * 26
for _ in range(n):
s = input().strip()
cnt[ord(s[0]) - ord('a')] += 1
print(max(cnt))
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.4-high.
posted:
last update: