-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamazon.cpp
More file actions
45 lines (43 loc) · 897 Bytes
/
amazon.cpp
File metadata and controls
45 lines (43 loc) · 897 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
using namespace std;using namespace std;
int findLongestLength(string s){
vector<int> a(26, -1), b(26);
int n = s.size(), r = 0;
for (int i = 0; i < n; ++i)
{
int j = s[i] - 'a';
if (a[j] == -1)
a[j] = i;
b[j] = i;
}
for (int k = 0; k < 26; ++k)
{
int i = a[k];
if (i == -1)
continue;
int m = b[k];
for (int j = i; j < n; ++j)
{
int x = a[s[j] - 'a'], y = b[s[j] - 'a'];
if (x < i)
break;
m = max(m, y);
if (m == j && j - i + 1 < n)
r = max(r, j - i + 1);
}
}
return r;
}
int main()
{
string s;
cin >> s;
cout << solve(s) << endl;
return 0;
}