-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1392_longest_prefix.cc
57 lines (53 loc) · 1.32 KB
/
1392_longest_prefix.cc
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
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <iostream>
#include <tuple>
#include <queue>
#include <stack>
using namespace::std;
class Solution {
public:
string longestPrefix(string s) {
int n = s.size();
int i = 0, j = n - 1,len = 0;
long long v0 = 0, v1 = 0, base = 1, mod = 1000000007;
for (i = 0, j = n - 1; i < n - 1; ++i, --j) {
v0 = (v0 * 26 + s[i] - 'a') % mod;
v1 = (v1 + (s[j] - 'a') * base) % mod;
if (v0 == v1)
len = i + 1;
/*if (v0 == v1) {
int l = 0, r = j;
while (l <= i && s[l] == s[r]) {
++l;
++r;
}
if (l > i)
len = i + 1;
}*/
base = (base * 26) % mod;
}
return s.substr(0, len);
}
string longestPrefix2(string s) {
int n = s.size();
vector<int> next(n, -1);
int j = -1;
for (int i = 1; i < n; ++i) {
while (j >= 0 && s[j + 1] != s[i]) {
j = next[j];
}
if (s[j + 1] == s[i])
j = j + 1;
next[i] = j;
}
return s.substr(0, next[n - 1] + 1);
}
};
int main()
{
return 0;
}