-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_unique_substring.cpp
More file actions
58 lines (40 loc) · 905 Bytes
/
longest_unique_substring.cpp
File metadata and controls
58 lines (40 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long int
#define FastIO ios::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
int longestUniqueChar(string str){
int cur_len = 1,n = str.size();
int max_len = 1;
int prev_index;
int *visited = new int[sizeof(int) * 256];
for(int i=0;i<256;i++) visited[i] = -1;
visited[str[0]] = 0;
for(int i=1;i<n;i++){
prev_index = visited[str[i]];
if(prev_index==-1 || i - cur_len > prev_index)
cur_len++;
else{
if(cur_len > max_len)
max_len = cur_len;
cur_len = i - prev_index;
cout<<cur_len<<" ";
}
visited[str[i]] = i;
}
if(cur_len > max_len)
max_len = cur_len;
free(visited);
return max_len;
}
int main(){
FastIO
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
string str;
cin>>str;
cout<<longestUniqueChar(str);
return 0;
}