GESP Level 3 practice, string exercises (Knowledge point 6 in the C++ Level 3 syllabus), difficulty ★★☆☆☆.
-
GESP Level 1 Practice Question List
-
GESP Level 1 Real Question List
-
GESP Level 2 Practice Question List
-
GESP Level 2 Real Question List
-
GESP Level 3 Practice Question List
-
GESP Level 3 Real Question List
-
GESP Level 4 Practice Question List
-
GESP Level 4 Real Question List
-
GESP Level 1-5 Syllabus Analysis
-
Essential Skills for GESP/CSP Programming
luogu-B2156 Longest Word 2
Problem Requirements
Problem Description
A simple English sentence ending with
<span>.</span>, with words separated by spaces, containing no abbreviations or other special forms, find the longest word in the sentence.
Input Format
A simple English sentence ending with
<span>.</span>(length not exceeding ), with words separated by spaces, containing no abbreviations or other special forms.
Output Format
The longest word in the sentence. If there are multiple, output the first one.
Input Output Example #1
Input #1
I am a student of Peking University.
Output #1
University
Problem Analysis
Solution Approach
- Read a line of English sentence and store it in a string variable
- Traverse the sentence to count word lengths:
- Define variables to record the current word length and the longest word length
- Define a variable to store the longest word
- When encountering a letter, increment the current word length by 1
- When encountering a space, compare the current word length with the longest word length
- If the current word is longer, update the longest word and its length
- Reset the current word length to 0
- Compare the last word length with the longest word length
- If the last word is longer, update the longest word
Complexity Analysis:
- Time complexity is , where n is the length of the sentence
- Space complexity is , where m is the length of the longest word
Example Code
#include <iostream>
#include <string>
int main() {
// Define a string variable to store the input sentence
std::string str;
// Read a line of input, including spaces
getline(std::cin, str);
// Record the length of the longest word
int max_count = 0;
// Record the length of the current word
int count = 0;
// Store the longest word
std::string max_str;
// Traverse the string, note that subtracting 1 is to avoid processing the last period
for (int i = 0; i < str.size() - 1; i++) {
// If not a space, it is part of a word
if (str[i] != ' ') {
count++;
} else {
// When encountering a space, check if the current word is the longest
if (count > max_count) {
// Extract the longest word and save it
max_str = str.substr(i - count, count);
max_count = count;
}
// Reset the current word length counter
count = 0;
}
}
// Handle the last word (excluding the period)
if (count > max_count) {
max_str = str.substr(str.size() - 1 - count, count);
}
// Output the longest word
std::cout << max_str << std::endl;
return 0;
}
Additionally, using <span>cin</span> for direct input makes the code more concise
#include <iostream>
#include <string>
int main() {
std::string str; // Variable to store each word read
int max_count = 0; // Record the length of the longest word
std::string max_str; // Store the longest word
// Loop to read words until EOF or input ends
while(std::cin >> str) {
// If the word ends with a period, remove the period
if (str.back() == '.') {
str.pop_back();
}
int str_length = str.length(); // Get the length of the current word
// If the current word length is greater than the recorded longest length
if (str_length > max_count) {
max_count = str_length; // Update the longest length
max_str = str; // Update the longest word
}
}
// Output the longest word
std::cout << max_str << std::endl;
return 0;
}
When debugging the above code locally, you may need to exit the loop using <span>Ctrl+Z</span>. However, on platforms like Luogu, you can test it through the provided tests. You can also automatically exit the loop by checking if the string ends with <span>.</span> and using <span>break</span> to exit the loop.
For detailed GESP syllabus, real question explanations, knowledge expansion, and practice lists, see:
[Pinned] [GESP] C++ Certification Learning Resource Summary
“luogu- series problems can be evaluated online at Luogu Problem Bank.
“bcqm- series problems can be evaluated online at Programming Enlightenment Problem Bank.