#include <iostream> #include <sstream> #include <vector> #include <string> using namespace std; int main() { string sentence, word; getline(cin, sentence); // 读取句子 cin >> word; // 读取单词 // 去掉句子末尾的句点 if (!sentence.empty() && sentence[sentence.size() - 1] == '.') { sentence.erase(sentence.size() - 1); } // 使用 stringstream 分割句子为单词 stringstream ss(sentence); vector<string> words; string temp; while (ss >> temp) { words.push_back(temp); } // 查找单词的位置 int position = -1; for (size_t i = 0; i < words.size(); ++i) { if (words[i] == word) { position = i + 1; // 位置从1开始 break; } } // 如果找到单词,输出位置 if (position != -1) { cout << position << endl; } else { // 如果未找到单词,计算单词字符的总个数 int totalChars = 0; for (size_t i = 0; i < words.size(); ++i) { totalChars += words[i].size(); } cout << totalChars << endl; } return 0; } /************************************************************** Problem: 1012 User: lidongbo Language: C++ Result: Accepted Time:7 ms Memory:2140 kb ****************************************************************/