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;
- }
- int main()
- {
- in >> s;
- n = strlen(s);
- int rasp = n - 1;
- int rasp_space = n - 1;
- for(int i = n / 2; i < n - 1; ++i)
- {
- if(test_char(i))
- {
- rasp = i;
- break;
- }
- }
- for(int i = n / 2; i < n - 1; ++i)
- {
- if(test_space(i))
- {
- rasp_space = i;
- break;
- }
- }
- out << s;
- if(rasp_space <= rasp)
- {
- for(int i = 1; i <= rasp; ++i)
- {
- out << s[rasp_space - (n - rasp_space) - i];
- }
- }
- else
- {
- for(int i = 0; i <= rasp; ++i)
- {
- out << s[rasp - (n - rasp) - i];
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement