最长不重复子串

题目描述:求最长不重复子串,如abcdefgegcsgcasse,最长不重复子串为abcdefg,长度为7


#include <iostream>
#include <list>
using namespace std;

//思路:用一个数组保存字符出现的次数。用i和j进行遍历整个字符串。
//当某个字符没有出现过,次数+1;出现字符已经出现过,次数+1,找到这个字符前面出现的位置的下一个位置,设为i
//并将之前的那些字符次数都-1。继续遍历,直到'\0'
int find(char str[],char *output)
{
	int i=0,j=0;
	int cnt[26]={0};
	int res=0,temp=0;
	char *out=output;
	int final;
	while(str[j]!='\0')
	{
		if(cnt[str[j]-'a']==0)
		{
			cnt[str[j]-'a']++;

		}
		else
		{
			cnt[str[j]-'a']++;
			while(str[i]!=str[j])
			{	
				cnt[str[i]-'a']--;
				i++;
			}
			cnt[str[i]-'a']--;
			i++;
		}	

		j++;
		temp=j-i;
		if(temp>res)
		{
			res=temp;
			final=i;
		}
	}
	//结果保存在output里面
	for(i=0;i<res;i++)
		*out++=str[final++];
	*out='\0';
	return res;
}
int main()
{
	char a[]="abcdefg";
	char b[100];
	int max=find(a,b);
	cout<<b<<endl;
	cout<<max<<endl;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值