双指针算法模板
for(i=0 , j=0;i<n;i++){
while(j<i && check(i,j))j++;
//具体逻辑
}
查找单词并输出(空格分隔)
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char str[1000];
gets(str);
int n = strlen(str);
for(i=1;i<=n;i++){
int j = i;//开始指向同一位置
while(j<n&&j!=' ')j++;//查找字符
for(int k=i;k<j;k++) cout<<str[k];//输出字符
cout<<endl;
i=j;
}
return 0;
}
每次i向后移动一位,j不会向前移动。
i,j区间中有重复数字时,i、j指向同一位置
i 从0到n-1枚举,j:j最远能到哪里
#include<iostream>
using namespace std;
const int N = 100010;
int a[N],s[N];
int n;
int main(){
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
int res = 0;
for(int i=0,j=0;i<n;i++){
s[a[i]]++;
while(s[a[i]]>1){//有重复元素
s[a[j]]--;//重复则移动
j++;
}
res = max(res,i-j+1);
}
cout<<res<<endl;
return 0;
}