-
Notifications
You must be signed in to change notification settings - Fork 0
/
p5.cpp
96 lines (83 loc) · 2.77 KB
/
p5.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
92
93
94
95
96
/**
* Medium
* https://leetcode.com/problems/longest-palindromic-substring/
*/
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
bool check_palindrome(string s) {
if (s.length() <= 1)
return true;
for (int i = 0, j = s.length()-1; i <= j; i++, j--)
if (s[i] != s[j]) return false;
return true;
}
class Solution {
public:
string longestPalindrome(string s) {
if (s.length() <= 1)
return s;
string curr("");
string longest_pal = "";
// string input1 = "abcdbbfcba";
for (int i = 0; i < s.length(); i++) {
for (int j = 1; j <= s.length()-i; j++) {
curr = s.substr(i, j);
// cout
// << "i: "
// << i
// << ", j: "
// << j
// << ", curr: "
// << curr
// << endl;
if (check_palindrome(curr))
if (curr.length() > longest_pal.length())
longest_pal = curr;
}
}
return longest_pal;
}
};
int main(void) {
Solution sol = Solution();
string input1 = "abcdbbfcba";
string input2 =
"apqjpwedlhmvvpexxnntxheeynxmgzwxhnhfdvziuxnuusymklgca\
cndoyhqkoahnkyaikohwkmnuphipftmzmihvmoetskioeypwjujvvu\
saxynzxxdugnebsisrtgeujkqkgwjuplijhluumqcdurovyjsbowmn\
qndejwkihzbbdyxjunkduyqeihektaknbmkzgnnmgywylulxwyywrv\
ieqfenjeljofkqqqisdjsbfkvqgahxwkfkcucvrbbpyhwkfztjdboav\
tfynrudneieelwlcezqsuhmllcsadcnoyemsfdlrijoyj";
string input3 =
"lcnvoknqgejxbfhijmxglisfzjwbtvhodwummdqeggzfczmetrdnoet\
mcydwddmtubcqmdjwnpzdqcdhplxtezctvgnpobnnscrmeqkwgiedhzsv\
skrxwfyklynkplbgefjbyhlgmkkfpwngdkvwmbdskvagkcfsidrdgwgmn\
qjtdbtltzwxaokrvbxqqqhljszmefsyewwggylpugmdmemvcnlugipqdj\
nriythsanfdxpvbatsnatmlusspqizgknabhnqayeuzflkuysqyhfxojh\
fponsndytvjpbzlbfzjhmwoxcbwvhnvnzwmkhjxvuszgtqhctbqsxnasn\
hrusodeqmzrlcsrafghbqjpyklaaqximcjmpsxpzbyxqvpexytrhwhmrk\
uybtvqhwxdqhsnbecpfiudaqpzsvfaywvkhargputojdxonvlprzwvrjl\
mvqmrlftzbytqdusgeupuofhgonqoyffhmartpcbgybshllnjaapaixdb\
bljvjomdrrgfeqhwffcknmcqbhvulwiwmsxntropqzefwboozphjectnu\
dtvzzlcmeruszqxvjgikcpfclnrayokxsqxpicfkvaerljmxchwcmxhtb\
witsexfqowsflgzzeynuzhtzdaixhjtnielbablmckqzcccalpuyahwow\
qpcskjencokprybrpmpdnswslpunohafvminfolekdleusuaeiatdqsoa\
tputmymqvxjqpikumgmxaxidlrlfmrhpkzmnxjtvdnopcgsiedvtfkltv\
plfcfflmwyqffktsmpezbxlnjegdlrcubwqvhxdammpkwkycrqtegepyx\
tohspeasrdtinjhbesilsvffnzznltsspjwuogdyzvanalohmzrywdwqq\
cukjceothydlgtocukc";
cout
<< "input3: "
<< input3
<< endl;
string ret3 = sol.longestPalindrome(input3);
cout
<< "ret3: "
<< ret3
<< endl;
cout << "done." << endl;
return 0;
}