UVa401——Palindromes

这题,很简单,判断是否是回文串,或者是镜像串,或者又是回文又是镜像。

字符的镜像如下:

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  

注意:每输出一行,都要空一行。

下面的是AC的代码:

#include 
#include 
using namespace std;

char maps[] = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";

char change(char a)
{
	if(a >= 'A' && a <= 'Z')
		return maps[a - 'A'];
	return maps[a - '0' + 25];
}

int main()
{
	bool flag, tag;
	char str[30];
	char temp[30];
	int i, j;
	while(cin >> str)
	{
		flag = tag = true;
		int length = strlen(str);
		for(i = 0; i < length; i++)
			temp[length - 1 - i] = str[i];
		temp[length] = '\0';
		for(i = 0; i < length; i++)
		{
			if(str[i] != temp[i])
				flag = false;
			if(str[i] != change(temp[i]))
				tag = false;
		}
		if(!flag && !tag)
			cout << str << " -- is not a palindrome." << endl;
		else if(flag && !tag)
			cout << str << " -- is a regular palindrome." << endl;
		else if(!flag && tag)
			cout << str << " -- is a mirrored string." << endl;
		else
			cout << str << " -- is a mirrored palindrome." << endl;
		cout << endl;
	}
	return 0;
}


你可能感兴趣的:(UVa)