Hdu-2668--尺取法

在这里插入图片描述在这里插入图片描述题目意思是给你一串长度为n的字符串,找出它最长的不含重复字母的子串,如果有相同长度的输出用最先出现的,输出这个子串的长度起始位置以及终止的位置
典型的尺取法问题

#include <cstdio>
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
int n;
const int N = 1e7+10;
char str[N];
bool vis[3000];
int main(){
	while(~scanf("%d",&n)){
		scanf("%s",str);
		memset(vis,false,sizeof(vis));
		int maxx = 0;int x,y;
		for(int r = 0,l = 0;r < n;r++){
			//如果右指针指向字母有重复,则一直移动左指针直至无重复 
			while(vis[(int)str[r]] && l <= r){
				vis[(int)str[l]] = false;
				l++;
			}
			vis[(int)str[r]] = true;
			if(r-l+1 > maxx){
				maxx = (r-l+1);
				x = l;y = r;
			}
		}
		printf("%d %d %d\n",maxx,x,y);
	}
	return 0;
}

发布了27 篇原创文章 · 获赞 0 · 访问量 353

猜你喜欢

转载自blog.csdn.net/weixin_44083561/article/details/103706113