#S0003. 词频统计

词频统计

1. Description

Create a word frequency analyzer. The user inputs a line of English words (space-separated). The program counts the occurrences of each word and outputs the top three most frequent words with their counts. If words have the same frequency, sort them alphabetically. If fewer than three words exist, output all.

2. Input & Output

Input:

  • A line of English words separated by spaces (assume all lowercase)

Output:

  • Three lines (or fewer), each formatted as word count
  • Sorted by frequency descending, then alphabetically ascending for ties

Example

# Input:  
apple banana orange apple banana apple  
# Output:  
apple 3  
banana 2  
orange 1  
# Input:  
cat dog cat bird dog cat dog  
# Output:  
cat 3  
dog 3  
bird 1