题意:n个女生,字符串表示每人衣服颜色,起点为0,求最长无重复字符的连续字串长度,并要输出较小的起点和终点
思路:对于每个位置,考虑当前位置为最后一个字符时,最长重复字符的连续字串长度。要用queue存前面无重复的字符,出现重复时便出栈到新的加入不在有重复。比较答案,存起终点即可
感想:这个题一开始多理解了一点,因为样例里只有小写字母,就理解为字符串内只有英文字母了。。导致存重时出错。。再就是一开始没有用queue,用数组存的,结果MLE了。。这个题虽然确实是个水题,但是也在卡内存,强制用queue。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f;
int n,ans,l,look[500],x,ll,rr,z;
char ch[10000005];
int main()
{
queue<int>q;
while(~scanf("%d",&n))
{
while(!q.empty()) q.pop();
gets(ch);
gets(ch);
memset(look,0,sizeof(look));
ans=0;
l=0;
for(int i=0;i<n;i++)
{
if(look[ch[i]]==1)
{
while(!q.empty())
{
z=q.front();
q.pop();
if(ch[z]==ch[i])
{
break;
}
look[ch[z]]=0;
}
q.push(i);
}
else {
q.push(i);look[ch[i]]=1;
if(i-q.front()+1>ans) {
ll=q.front();rr=i;
ans=i-q.front()+1;
}
}//cout<<i<<" "<<l<<" "<<i<<" "<<ll<<" "<<rr<<" "<<ans<<endl;
}
printf("%d %d %d\n",ans,ll,rr);
}
}