Description
给出一个字符串,问这个字符串所有子串中出现不同字母数不超过k的最长的一个子串的起始位置和终止位置
Input
第一行一整数k,之后一个字符串,串长不超过1e5(1<=k<=26)
Output
输出两个整数表示满足条件的最长子串起始和终止位置
Sample Input
3
abrakadabra
Sample Output
4 8
Solution
简单尺取
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 111111
int k,n;
char s[maxn];
map<int,int>m;
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
while(~scanf("%d",&k))
{
scanf("%s",s);
n=strlen(s);
int ansl,ansr,ans=0,res=0,l=0,r=-1;
m.clear();
while(1)
{
if(res<=k&&r-l+1>ans)ans=r-l+1,ansl=l,ansr=r;
if(res<=k)
{
if(r==n-1)break;
r++;
if(!m[s[r]-'a'])res++;
m[s[r]-'a']++;
}
else
{
m[s[l]-'a']--;
if(!m[s[l]-'a'])res--;
l++;
if(l==n)break;
}
}
printf("%d %d\n",ansl+1,ansr+1);
}
return 0;
}