双指针算法i移动到j指向的空格处于是只需要移动i = j将i指向空格处
而在AcWing 771. 字符串中最长的连续出现的字符中移动i = j - 1是为了让i两字母不同的交界处
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
string str;
getline(cin, str);
int n = str.size();
int cnt = 0;
string res;
for (int i = 0; i < n; i ++ )
{
string a;
int j = i;
while(j < n && str[j] != ' ' && str[j] !='.') a = a + str[j],j ++;
if(j - i > cnt) cnt = j - i, res = a;
i = j;//此处加上i = j 降低时间复杂度
}
cout << res << endl;
return 0;
}