大致思路是

将读入的数据转换成小写,转换的函数是使用了transform这个函数

具体用法可以看 transform

然后就是进行单词查找了。

文章可以看成是很多段组成,每一段都是由一片连续的空格 + 一片连续的字母构成。

所以,先去掉字母前面多余的空格,让指针指向第一个单词的第一个字母,

然后进行判断是否找到了一个单词,进行统计

然后移动到移动到下一个空格的开始

这样保证每次最外层的循环都是从每段中的第一个空格开始,到这一段的最后一个字母结束

从而下一次循环就是从下一段的第一个空格开始,到下一段的最后一个字母结束


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() {
   
    string world, text, tmp;
    cin >> world;getchar();
    getline(cin, text);

    transform(world.begin(),world.end(),world.begin(),::tolower);
    transform(text.begin(),text.end(),text.begin(),::tolower);

    const size_t W = world.size(), T = text.size();
    int cnt = 0, begin = -1;
    size_t i = 0, j = 0;
    
    while (i < T) {
   
        while (text[i] == ' ' && i < T) ++i;
        j = 0;
        while (text[i] == world[j]) ++j, ++i;
        if (j == W && (text[i] == ' ' || text[i] == '\0')) {
   
            ++cnt;
            if (begin == -1) begin = i - W;
        } 
        while (text[i] != ' ' && i < T) ++i;
    }

    if (begin == -1) cout << -1 << endl;
    else cout << cnt << " " << begin << endl;
    
    return 0;
}