核心思想在于查找两个循环的关系
主要分为指向一个序列或指向两个序列
1.基础模板
for(int i=0,j=0;i<n;i++)
{
while(j<i&&check(i,j))j++;
}
2.具体应用
Ⅰ.将含有空格字符串中的单词分行输出
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[1000];
gets(str);
int n=strlen(str);
for(int i=0;i<n;i++)
{
int j=i;
while(j<n&&str[j]!=' ')j++;
for(int k=i;k<j;k++)cout<<str[k];
cout<<endl;
i=j;
}
return 0;
}
Ⅱ.最长连续不重复子序列
#include<iostream>
using namespace std;
const int N=100010;
int a[N],b[N];//开两个数组 ,b[]为计数器
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
int k=0;//记录不连续的个数
for(int i=0,j=0;i<n;i++)//i指针向前移动
{
b[a[i]]++;//b来记录a中出现不同数的个数
while(b[a[i]]>1)//当a[i]中出现重复的数时
{
b[a[j]]--;//将j指针当前指向数据的计数记录减去
j++;//j指针向前移动
}
k=max(k,i-j+1);//i-j+1为两指针之间的距离,max()返回最大值
}
cout<<k<<endl;
return 0;
}