首页 > 代码库 > UVA 401-- Palindromes--串处理
UVA 401-- Palindromes--串处理
Palindromes
Palindromes |
A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from right to left.
A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string. For example, the string "3AIAE" is a mirrored string because "A" and "I"are their own reverses, and "3" and "E" are each others‘ reverses.
A mirrored palindrome is a string that meets the criteria of a regular palindrome and the criteria of a mirrored string. The string"ATOYOTA" is a mirrored palindrome because if the string is read backwards, the string is the same as the original and because if each of the characters is replaced by its reverse and the result is read backwards, the result is the same as the original string. Of course, "A", "T","O", and "Y" are all their own reverses.
A list of all valid characters and their reverses is as follows.
Character | Reverse | Character | Reverse | Character | Reverse |
A | A | M | M | Y | Y |
B | N | Z | 5 | ||
C | O | O | 1 | 1 | |
D | P | 2 | S | ||
E | 3 | Q | 3 | E | |
F | R | 4 | |||
G | S | 2 | 5 | Z | |
H | H | T | T | 6 | |
I | I | U | U | 7 | |
J | L | V | V | 8 | 8 |
K | W | W | 9 | ||
L | J | X | X |
Note that O (zero) and 0 (the letter) are considered the same character and therefore ONLY the letter "0" is a valid character.
Input
Input consists of strings (one per line) each of which will consist of one to twenty valid characters. There will be no invalid characters in any of the strings. Your program should read to the end of file.Output
For each input string, you should print the string starting in column 1 immediately followed by exactly one of the following strings.STRING | CRITERIA |
" -- is not a palindrome." | if the string is not a palindrome and is not a mirrored string |
" -- is a regular palindrome." | if the string is a palindrome and is not a mirrored string |
" -- is a mirrored string." | if the string is not a palindrome and is a mirrored string |
" -- is a mirrored palindrome." | if the string is a palindrome and is a mirrored string |
Note that the output line is to include the -‘s and spacing exactly as shown in the table above and demonstrated in the Sample Output below.
In addition, after each output line, you must print an empty line.
Sample Input
NOTAPALINDROME ISAPALINILAPASI 2A3MEAS ATOYOTA
Sample Output
NOTAPALINDROME -- is not a palindrome. ISAPALINILAPASI -- is a regular palindrome. 2A3MEAS -- is a mirrored string. ATOYOTA -- is a mirrored palindrome.
依旧是uva的串处理,这个题咋一看很难其实很简单,就是在原先的传统回文串的定义加了一个概念叫镜像串,这样一组合就有4种情况:非回文串,镜像串,回文串,镜像回文串。
只需要写两个函数判断是否回文串 是否是镜像串即可。回文串就不多说了很简单,这里说一下镜像串的概念及判定。举个例子,比如说 3AIAE 这么一个串,我们首先根据上面给定的表格中对应关系求出这个串的倒置(上面有个表格,里面有对应关系,比如3的倒置是E)我们倒置后可以得到 EAIA3 如果这个倒置后的串跟原始串的逆序一样,那么次串是镜像串,反之。。
#include<cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <cstring> #include <cctype> using namespace std; char s[110]; //将对应关系写到两个数组里,一个字母表,一个数字表 char alresve[100] = {'A', '-1', '-1', '-1', '3', '-1', '-1', 'H', 'I', 'L', '-1', 'J', 'M', '-1', 'O', '-1', '-1', '-1', '2', 'T', 'U', 'V', 'W', 'X', 'Y', '5'}; char numresve[100] = {'1', 'S', 'E', '-1', 'Z', '-1', '-1', '8', '-1'}; int is_palindrome() { int len = strlen(s); for (int i = 0; i < len; i++) if (s[i] != s[len - 1 - i]) { return 0; } return 1; } int is_mirrored() { int i, len = strlen(s); char t[50], tem; int p = 0; for (i = 0; i < len; i++) { if (isalpha(s[i])) { if (alresve[s[i] - 'A'] != '-1') { t[p++] = alresve[s[i] - 'A']; } else { t[p++] = s[i]; } } else if (isdigit(s[i])) { if (numresve[s[i] - '1'] != '-1') { t[p++] = numresve[s[i] - '1']; } else { t[p++] = s[i]; } } } t[p] = '\0'; for (i = 0; i < p / 2; i++) { tem = t[i]; t[i] = t[p - 1 - i]; t[p - 1 - i] = tem; } if (strcmp(t, s) == 0) { return 1; } else { return 0; } } int main() { while (cin >> s) { int f1 = 0, f2 = 0; if (is_palindrome()) { f1 = 1; } if (is_mirrored()) { f2 = 1; } if (f1 && f2) { cout << s << " -- is a mirrored palindrome." << endl << endl; } else if (f1 && !f2) { cout << s << " -- is a regular palindrome." << endl << endl; } else if (!f1 && f2) { cout << s << " -- is a mirrored string." << endl << endl; } else if (!f1 && !f2) { cout << s << " -- is not a palindrome." << endl << endl; } } return 0; }