Optional: subsequences and useful tools
Distinguish consecutive pieces from ordered selections, then recognize safer library conveniences.
Consecutive is different from in-order
A subarray and a substring are consecutive pieces. A subsequence may skip values, but must preserve their original order. In abcde, bcd is a substring; ace is a subsequence but not a substring; ca is not a subsequence.
A prefix begins at the start and a suffix reaches the end. To check a subsequence, scan the text with one index and keep another count of pattern characters already matched. Match the earliest available character, leaving as much remaining text as possible for later matches.
An empty pattern is a subsequence of any text. A nonempty pattern is not a subsequence of empty text. Guard matched < needed before reading pattern[matched], including after the entire pattern has matched.
#include <iostream>
#include <string>
int main() {
std::string pattern, text;
std::getline(std::cin, pattern);
std::getline(std::cin, text);
int needed = static_cast<int>(pattern.size());
int n = static_cast<int>(text.size());
int matched = 0;
for (int i = 0; i < n; ++i) {
if (matched < needed && text[i] == pattern[matched]) {
++matched;
}
}
std::cout << (matched == needed ? "YES" : "NO") << '\n';
return 0;
}Fixed containers and range-based loops
std::array<int, 4> from <array> is a fixed-size standard-library wrapper supporting size(), copying, and content comparison. Its size is still a compile-time choice. A partly used std::array<int, 1000> still reports size 1000, so shorter inputs need an active length.
for (char c : s) visits copies of the characters. for (char& c : s) provides writable references to the stored characters. A range loop over a raw array visits its entire capacity, not just your active n; keep index loops when the active range is shorter.
Edit pieces and ask text questions
String insert(pos, text), erase(pos, count), and replace(pos, count, text) modify the current string. A starting position beyond size() is out of range. After changes, old lengths and saved indices may no longer identify the same characters.
starts_with and ends_with ask about a beginning or ending; contains asks about occurrence anywhere. The first two are available since C++20 and contains since C++23. They return a Boolean, unlike find, which returns a position or npos.
#include <iostream>
#include <string>
int main() {
std::string s = "team";
s.insert(0, "HNU ");
std::cout << s << '\n';
s.erase(0, 4);
s.replace(0, 4, "ICPC");
std::cout << s << '\n';
std::cout << std::boolalpha << s.starts_with("IC") << '\n';
std::cout << s.ends_with("PC") << '\n';
std::cout << s.contains("CP") << '\n';
return 0;
}Character tools and terminators
<cctype> offers isdigit, isalpha, isspace, tolower, and toupper. When passing a possibly signed char, convert it to unsigned char first. For example, static_cast<char>(std::tolower(static_cast<unsigned char>(s[i]))) safely stores the conversion result. These functions may depend on the active C locale and are not a complete Unicode solution.
A C-style string needs a terminating null character: char text[4] = "HNU" stores H, N, U, and '\0'. char letters[3] = {'H', 'N', 'U'} has no terminator and must not be passed to operations expecting one. std::string tracks its length, so prefer it for this training.
These optional tools are for recognition after the core scans make sense. Explain the bounds, initial state, and result of an ordinary loop before replacing it with a shorter spelling.
For text abc, which statement is correct?
Turn the idea into code
Optional: read a full ASCII line and replace every hyphen with a plus sign using a range-based loop. Explain why char c alone would not modify the string.
Give me a hint
Use a writable reference to each stored character.
Show the explained solution
char& c aliases the current character. Assigning through it changes the string; a plain char c would be only a copy.
#include <iostream>
#include <string>
int main() {
std::string s;
std::getline(std::cin, s);
for (char& c : s) {
if (c == '-') { c = '+'; }
}
std::cout << s << '\n';
return 0;
}Your notes
Mark complete when you can explain it yourself.