generated from OPCODE-Open-Spring-Fest/template
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e02c621
commit 682a94a
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,41 @@ | ||
//Write your code here | ||
//Write your code here | ||
#include <iostream> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
string func(const string& s) { | ||
int n = s.length(); | ||
int max_length = 0; | ||
int max_index = -1; | ||
|
||
for (int i = 0; i < n; ++i) { | ||
for (int j = i + 1; j < n; ++j) { | ||
int length = 0; | ||
while (j + length < n && s[i + length] == s[j + length]) { | ||
++length; | ||
} | ||
if (length > max_length) { | ||
max_length = length; | ||
max_index = i; | ||
} | ||
} | ||
} | ||
|
||
if (max_length == 0) { | ||
return "-1"; | ||
} else { | ||
return s.substr(max_index, max_length); | ||
} | ||
} | ||
|
||
int main() { | ||
string s; | ||
cin>>s; | ||
|
||
string result = func(s); | ||
cout << result << endl; | ||
|
||
return 0; | ||
} | ||
|
Binary file not shown.