// UVa 401 - Palindromes #include <iostream> #include <map> #include <string> using namespace std; map<char,char> mirror; void init_mirror() { mirror['A'] = 'A'; mirror['E'] = '3'; mirror['H'] = 'H'; mirror['I'] = 'I'; mirror['J'] = 'L'; mirror['L'] = 'J'; mirror['M'] = 'M'; mirror['O'] = 'O'; mirror['S'] = '2'; mirror['T'] = 'T'; mirror['U'] = 'U'; mirror['V'] = 'V'; mirror['W'] = 'W'; mirror['X'] = 'X'; mirror['Y'] = 'Y'; mirror['Z'] = '5'; mirror['1'] = '1'; mirror['2'] = 'S'; mirror['3'] = 'E'; mirror['5'] = 'Z'; mirror['8'] = '8'; } bool check_palindrome(const string & input) { int length = input.length(); for (int left=0, right = length-1; left<=right; left++, right--) { if (input[left] != input[right]) return false; } return true; } bool check_mirrored(const string & input) { int length = input.length(); for (int left=0, right = length-1; left<=right; left++, right--) { if (input[left] != mirror[input[right]]) return false; } return true; } string get_answer(bool palindrome, bool mirrored) { if (!palindrome && !mirrored) { return " -- is not a palindrome."; } else if (palindrome && !mirrored) { return " -- is a regular palindrome."; } else if (!palindrome && mirrored) { return " -- is a mirrored string."; } else if (palindrome && mirrored) { return " -- is a mirrored palindrome."; } return ""; } int main() { init_mirror(); string input_string; while (cin >> input_string) { bool palindrome = check_palindrome(input_string); bool mirrored = check_mirrored(input_string); string answer = get_answer(palindrome, mirrored); cout << input_string << answer << endl << endl; } return 0; }
Monday, October 17, 2016
UVa 401 - Palindromes
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment