Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstring>
- using namespace std;
- ifstream in("palindrom.in");
- ofstream out("palindrom.out");
- const int lMax = 200005;
- char s[lMax];
- int n;
- inline bool test_char(int x) //daca caracterul de pe poz x poate fi mijlocul unui palindrom
- {
- for (int i = 1; x + i < n; ++i)
- if (s[x + i] != s[x - i])
- return false;
- return true;
- }
- inline bool test_space(int x) //daca spatiul de pe poz x poate fi mijlocul unui palindrom
- {
- for (int i = 1; x + i <= n; ++i)
- {
- if (s[x + i - 1] != s[x - i])
- {
- return false;
- }
- }
- return true;
- }
- void Write(int rasp)
- {
- for (int i = 0; rasp - (n - rasp) - i >= 0; ++i)
- {
- out << s[rasp - (n - rasp) - i];
- }
- }
- int main()
- {
- in >> s;
- n = strlen(s);
- int rasp = n-1;
- int rasp_space = n;
- for (int i = n / 2; i < n - 1; ++i)
- {
- if (test_char(i))
- {
- rasp = i;
- break;
- }
- }
- for (int i = n / 2; i < n; ++i)
- {
- if (test_space(i))
- {
- rasp_space = i;
- break;
- }
- }
- out << s;
- bool flag = 0;
- if (rasp_space <= rasp)
- flag = 1;
- rasp = min(rasp_space, rasp);
- for (int i = 0; rasp - (n - rasp) - i - flag>= 0; ++i)
- {
- out << s[rasp - (n - rasp) - i - flag];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement