Repository navigation
Expand file tree
/
Copy path0005.cpp
More file actions
122 lines (100 loc) · 2.49 KB
/
Copy path0005.cpp
File metadata and controls
122 lines (100 loc) · 2.49 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Longest Palindromic Substring - GeeksforGeeks
// https://www.geeksforgeeks.org/longest-palindrome-substring-set-1/
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
bool isPalindrome(string s) {
if (s[0] == s[s.size()-1]) {
return true;
} else {
return false;
}
}
string subLongestPalindrome(vector<vector<int>> v_old, string s) {
int start, end, maxlen;
vector<vector<int>> v_new = {};
string ss, subs;
if (v_old.empty()) {
subs = "";
return subs;
}
while (true) {
maxlen = v_old[0][1];
for (int i=0; i<v_old.size(); ++i) {
start = v_old[i][0];
end = start + maxlen - 1;
if (start-1>=0 && end+1<=s.size()-1) {
ss = s.substr(start-1, maxlen+2);
if (isPalindrome(ss)) {
v_new.push_back({start-1, maxlen+2});
}
}
}
if (v_new.empty()) {
break;
} else {
v_old = v_new;
v_new = {};
maxlen += 2;
}
}
subs = s.substr(v_old[0][0], v_old[0][1]);
return subs;
}
class Solution {
public:
string longestPalindrome(string s) {
vector<vector<int>> init_v_odd = {};
vector<vector<int>> init_v_even = {};
string subs;
if (s.size()<=1){
subs = s;
return subs;
} else if (s.size()==2) {
if (s[0]==s[1]){
subs = s;
} else {
subs = s[0];
}
return subs;
}
for (int i=0; i<s.size(); ++i) {
if (i>=0 && i<=s.size()-1) {
init_v_odd.push_back({i,1});
}
if (i>=0 && i+1<=s.size()-1) {
if (s[i]==s[i+1]){
init_v_even.push_back({i,2});
}
}
}
string subs_odd, subs_even;
subs_odd = subLongestPalindrome(init_v_odd, s);
subs_even = subLongestPalindrome(init_v_even, s);
if (subs_odd.size() > subs_even.size()){
return subs_odd;
} else {
return subs_even;
}
}
};
int main() {
string sin;
// FILE * fp = fopen("0005.txt", "r");
ifstream fp;
fp.open("0005.txt");
vector<string> vs;
while (getline(fp, sin, '\n')) {
// printf("%s\n", sin.c_str());
vs.push_back(sin);
}
fp.close();
for (int i=0; i<vs.size(); ++i) {
string sss = longestPalindrome(vs[i]);
printf("%s\n", sss.c_str());
}
return 0;
}