string,
string,
1、编号3 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
int f1(string s,int i,int j){
int exist[256]={0};
int res=0;
exist[s[0]]=1;
while(j<s.length()){
if(exist[s[j]]){
res=max(res,j-i);
memset(exist,0,sizeof(exist));
i++;
j=i+1;
exist[s[i]]=1;
}
else{
exist[s[j]]=1;
j++;
}
}
res =max(res,j-i);
return res;
}
int main(){
//statement
string s;
int i=0,j=1;
int length;
//init
cin>>s;
length = f1(s,i,j);
cout<<length;
}
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
int f1(string s,int i,int j){
int exist[256]={0};
int res=0;
exist[s[0]]=1;
while(j<s.length()){
if(exist[s[j]]){
res=max(res,j-i);
while(s[i]!=s[j]){
exist[s[i]]=0;
i++;
}
i++;
j++;
}
else{
exist[s[j]]=1;
j++;
}
}
res =max(res,j-i);
return res;
}
int main(){
//statement
string s;
int i=0,j=1;
int length;
//init
cin>>s;
length = f1(s,i,j);
cout<<length;
}
相关文章
- 暂无相关文章
用户点评