GESP C++ Level 3 Practice: String Substring Comparison

GESP Learning Resource List
Real Questions Practice Questions Syllabus Analysis
Level 1 Real Questions List Level 1 Practice Questions List Level 1-5 Syllabus Analysis
Level 2 Real Questions List Level 2 Practice Questions List GESP/CSP Essential Programming Skills
Level 3 Real Questions List Level 3 Practice Questions List
Level 4 Real Questions List Level 4 Practice Questions List
Level 5 Real Questions List Level 5 Practice Questions List
CSP Learning Resource List
CSP-XL
2025 Liaoning CSP-XL Semi-Final Real Questions Analysis

GESP C++ Level 3 practice, substring extraction practice, difficulty ★★☆☆☆.

luogu-B3769 [Language Monthly Competition 202305] String Substring Comparison

Problem Requirements

Problem Background

In this problem, we use to represent the string formed by concatenating characters from the th character to the th character of the string. For example, if , then .

Problem Description

Given two strings and , there are queries.

For each query, given and , please determine whose dictionary order is smaller between and .

Input Format

The first line is a string .The second line is a string .The third line is an integer representing the number of queries . Following that, lines, each containing four integers , representing one query.

Output Format

For each query, output one line with a string:

  • If has a smaller dictionary order, output >.
  • If has a smaller dictionary order, output >.
  • If both have the same dictionary order, output .

Input Output Example #1

Input #1

Yifusuyi
yifusuYi
3
1 2 7 8
1 2 1 2
7 8 7 8

Output #1

ovo
yifusuyi
erfusuer

Notes/Tips

Data Scale and Constraints

Let represent the length of , and represent the length of .

  • For , .
  • For , .
  • For , , and , the input strings only contain uppercase and lowercase English letters.

Problem Analysis

Solution Approach

The solution approach for this problem is as follows:

  1. Problem Analysis:
    • Input two strings and , and the number of queries
    • Each query provides four integers , indicating the substring range to compare
    • Compare the dictionary order of and .
  1. Solution Method:
    • Core Idea:
      • Use the string’s substr function to extract substrings
      • Directly use string comparison operators to compare dictionary order
      • Output the corresponding string based on the comparison result
    • Implementation Method:
      • Read the two original strings and
      • Loop to process queries
      • For each query, extract and compare substrings
  1. Implementation Points:
  • String length range:
  • Query count range:
  • Valid substring range:,
  • Strings only contain uppercase and lowercase letters

Complexity Analysis:

  • Time Complexity:, where q is the number of queries, and L is the maximum substring length
  • Space Complexity:, requires storage for the original strings and substrings

Example Code

#include <iostream>
#include <string>
int main() {
    // Declare two string variables s and t to store the input strings
    std::string s, t;
    // Declare an integer variable q to store the number of queries
    int q;
    // Read the two input strings and the number of queries
    std::cin >> s >> t >> q;
    // Loop to process q queries
    for (int i = 0; i < q; i++) {
        // Declare four integer variables to store the query positions
        int l1, r1, l2, r2;
        // Read the four position parameters for each query
        std::cin >> l1 >> r1 >> l2 >> r2;
        // Extract substrings from s, note that indices start from 0, so subtract 1
        std::string s_sub = s.substr(l1 - 1, r1 - l1 + 1);
        // Extract substrings from t
        std::string t_sub = t.substr(l2 - 1, r2 - l2 + 1);
        // Compare the dictionary order of the two substrings
        if (s_sub < t_sub) {
            // s's substring has a smaller dictionary order
            std::cout << "yifusuyi" << std::endl;
        } else if (s_sub > t_sub) {
            // t's substring has a smaller dictionary order
            std::cout << "erfusuer" << std::endl;
        } else {
            // Both substrings have the same dictionary order
            std::cout << "ovo" << std::endl;
        }
    }
    return 0;
}

【Recommended】Mobile Version Recommendation:【GESP】C++ Certification Learning Resource Summary

【Recommended】Desktop Version Recommendation: GESP/CSP Examination Material Website:https://wiki.coderli.com/ Dictionary-style resource organization, categorized for easy access.

GESP C++ Level 3 Practice: String Substring Comparison

luogu-” series problems can be evaluated online atLuogu Problem Bank.

bcqm-” series problems can be evaluated online atProgramming Enlightenment Problem Bank.

.cls-1{fill:#001e36;}.cls-2{fill:#31a8ff;}.cls-1{fill:#001e36;}.cls-2{fill:#31a8ff;}

Leave a Comment