#include <iostream> #include <string> using namespace std; int main() { std::string s; std::getline(cin, s); int i = s.length() - 1; // 从末尾开始,定位最后一个单词的末位索引 while (i >= 0 && s[i] == ' ') { i--; } int end = i; // 继续向前,查找单词的开头,注意,这里结束后得到的索引是最后一个单词的上一个字符 // 所以在下面的子串截取时,正确的位置应该是i+1...