-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecretWord_UVA12467OJ.cpp
91 lines (76 loc) · 2.05 KB
/
secretWord_UVA12467OJ.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int tcc; cin >> tcc;
for (int c = 1; c <= tcc; ++c)
{
string s; cin >> s;
int sizeS = s.size();
int l = 1, r = sizeS;
while (l <= r)
{
int m = (l + r) / 2;
string t = s.substr(0, m); reverse(t.begin(), t.end());
int sizeT = t.size();
int _i = 1, _j = 0;
vector<int> next(sizeT, 0); next[0] = 0;
while (_i <= sizeT - 1)
{
while (_j > 0 && t[_i] != t[_j])
{
if (_j > 0) _j = next[_j - 1];
if (_j == -1) break;
}
if (t[_i] == t[_j]) next[_i] = _j + 1;
++_i;
++_j;
}
bool flag = false;
int i = 0, j = 0;
while (i <= sizeS - 1)
{
if (s[i] != t[j])
{
if (j == 0)
{
++i;
}
else
{
j = next[j - 1];
}
}
else
{
if (j == sizeT - 1)
{
flag = true;
++i;
j = 0;
break;
}
else
{
++i;
++j;
}
}
}
if (flag == true)
{
l = m + 1;
}
else
{
r = m - 1;
}
}
string ans = s.substr(0, l - 1); reverse(ans.begin(), ans.end());
cout << ans << '\n';
}
cout.flush();
return 0;
}