Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created September 14, 2016 01:09
Show Gist options
  • Save dgodfrey206/2a2e5bd7ff8a499f1165e88bc2357451 to your computer and use it in GitHub Desktop.
Save dgodfrey206/2a2e5bd7ff8a499f1165e88bc2357451 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
// returns true if A is a fading palindome
bool ifp(char* A) {
int i, n = strlen(A);
for (i = 0; i < n/2; ++i) {
// we need to check the palindome invariant A[i] == A[n-i-1] and
// if either characters are '.' we always assume they are the same.
if ((A[i] != A[n-i-1] && A[i] != '.' && A[n-i-1] != '.')) return false;
}
// by this point either the entire string has been checked or there is
// a single character left in the center of the string (either '.' or something else).
// this still makes a valid palindrome.
return true;
}
void test(char* A) {
int i, n = strlen(A);
// if it's not a fading palidrome output -1
if (!ifp(A)) cout << -1;
else {
for (i = 0; i < n; ++i) {
// print the string, but replace '.' with the character on the other
// half of the string, creating a palindrome.
// The only case where we print any character from a-z is when the
// '.' is in the center of the string or characters on both halfs
// of the strings are '.'s.
cout <<
((A[i] == '.') ?
((i == n/2 || A[n-i-1] == '.') ?
'a' : // arbitrary character
A[n-i-1]) :
A[i]);
}
}
cout << '\n';
}
int main() {
char str[12345+1];
int t, i;
cin >> t;
for (i = 0; i < t; ++i) {
cin >> str;
test(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment