This is a Level 3 exercise in GESP, focusing on string manipulation (Knowledge Point 6 in the C++ Level 3 syllabus), Difficulty ★★☆☆☆.
luogu-B2118 Substring Verification
Problem Requirements
Problem Description
Input two strings and verify if one string is a substring of the other.
Input Format
Two lines, each containing one string.
Output Format
If the first string is a substring of the second string, output
<span>(s1) is substring of (s2)</span>;Otherwise, if the second string is a substring of the first string, output
<span>(s2) is substring of (s1)</span>;If neither is true, output
<span>No substring</span>.
Input/Output Example #1
Input #1
abc
dddncabca
Output #1
abc is substring of dddncabca
Input/Output Example #2
Input #2
aaa
bbb
Output #2
No substring
Notes/Tips
For the data of , the string length is within .
Problem Analysis
Solution Approach
-
The problem requires normalizing the case format of drug names.
-
Normalization rules analysis:
- The first letter needs to be capitalized
- All other letters need to be lowercase
- Numbers and special characters (like “-“) remain unchanged
Specific approach:
- Read the number of drugs and the drug names
- Process each drug name:
- Check the first character; if it is a letter, convert it to uppercase
- Convert the remaining characters to lowercase
- Keep numbers and special characters unchanged
- Output the processed result
- Time complexity analysis:
- Each drug name’s characters need to be traversed,
- where is the number of drugs, is the maximum length of the drug names
- Space complexity is , requiring storage for n strings of length m
Example Code
#include <cstdio>
#include <iostream>
#include <string>
int main() {
// Declare two string variables to store input
std::string str1;
std::string str2;
// Use getline to read two lines of input, each containing one string
std::getline(std::cin, str1);
std::getline(std::cin, str2);
// Check if str1 contains str2
if (str1.find(str2) != std::string::npos) {
// If str2 is a substring of str1, output the result in the specified format
printf("%s is substring of %s", str2.c_str(), str1.c_str());
}
// Check if str2 contains str1
else if (str2.find(str1) != std::string::npos) {
// If str1 is a substring of str2, output the result in the specified format
printf("%s is substring of %s", str1.c_str(), str2.c_str());
}
// If neither string is a substring of the other
else {
std::cout << "No substring";
}
return 0;
}
For more details on GESP levels, past exam questions, knowledge expansion, and practice lists, see:
【Top】【GESP】C++ Certification Learning Resources Summary
The “luogu-” series problems can be evaluated online atLuogu Problem Bank.
The “bcqm-” series problems can be evaluated online atProgramming Enlightenment Problem Bank.