-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
54 lines (48 loc) · 1.14 KB
/
main.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
// Author : Qi Zhang
// Date : 2018-12-11
#include <bits/stdc++.h>
using namespace std;
vector<int> getNext(string &p){
int len = p.size();
vector<int> next(len);
next[0] = 0 ;
int i = 0 , j = 0 ;
for(j = 1 ; j < len ; j++) {
while( p[i] != p[j] && i > 0 )
i = next[i-1] ;
if(p[i] == p[j]) {
i++ ;
next[j] = i ;
}
else
next[j] = 0 ;
}
return next;
}
int main()
{
string s;
while (getline(cin, s)) {
vector<int> next = getNext(s);
//for(auto a: next) cout << a << endl;
int n = s.size(), count = 1;
string ans;
for(int i = 0; i < n; i++){
if(i == 0){
ans += s[i];
next[i] = 1;
continue;
}
if(next[i] == 0){
count++;
next[i] = next[i-1] + 1;
if(next[i] > ans.size()) ans += s[i];
}
else{
if(next[i] > ans.size() && count > 1) ans = s.substr(0, next[i]);
}
}
cout << ans << endl;
}
return 0;
}