2022 sector 10 statements
This commit is contained in:
75
2022/sector/12/palindrome/palindrome.cpp
Normal file
75
2022/sector/12/palindrome/palindrome.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ifstream input_stream;
|
||||
ofstream output_stream;
|
||||
|
||||
|
||||
bool is_palindrome(string s) {
|
||||
uint8_t s_len = s.length();
|
||||
|
||||
for (uint8_t i = 0; i < (s_len / 2); i++) {
|
||||
if (s[i] != s[s_len - i - 1]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
input_stream.open("input.txt");
|
||||
output_stream.open("output.txt");
|
||||
|
||||
string max_pal, tmp;
|
||||
string inp = "";
|
||||
|
||||
// File method:
|
||||
while (true) {
|
||||
input_stream >> tmp;
|
||||
if (tmp == "") {
|
||||
break;
|
||||
}
|
||||
inp += " " + tmp;
|
||||
tmp = "";
|
||||
}
|
||||
|
||||
uint8_t max_pal_len = 0;
|
||||
|
||||
uint8_t inp_len = inp.length();
|
||||
|
||||
|
||||
for (uint8_t i = 0; i < (inp_len - 1); i++) {
|
||||
for (uint8_t j = inp_len - i; j >= 2; j--) {
|
||||
if (j > max_pal_len) {
|
||||
string sub = inp.substr(i, j);
|
||||
if (is_palindrome(sub)) {
|
||||
max_pal_len = j;
|
||||
max_pal = sub;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// File method:
|
||||
// if (max_pal_len == 0) {
|
||||
// output_stream << "Nu se contine" << endl;
|
||||
// } else {
|
||||
// output_stream << max_pal << endl;
|
||||
// }
|
||||
// Console method:
|
||||
if (max_pal_len == 0) {
|
||||
cout << "Nu se contine" << endl;
|
||||
} else {
|
||||
cout << max_pal << endl;
|
||||
}
|
||||
|
||||
input_stream.close();
|
||||
output_stream.close();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user