HDU 1318(Palindromes)

基础题,使用 <algorithm> 的库函数 reverse_copy,将原始字符串逆序存储,然后依次比较是否为回文和镜像。

#include <iostream>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXL = 25;

char before[MAXL], after[MAXL]; //原始字符串,逆序后的字符串
map<char, char> mp; //原字符-镜像

//初始化对应镜像
void mirroredInit()
{
	mp['A'] = 'A'; mp['E'] = '3'; mp['H'] = 'H'; mp['I'] = 'I';
	mp['J'] = 'L'; mp['L'] = 'J'; mp['M'] = 'M'; mp['O'] = 'O';
	mp['S'] = '2'; mp['T'] = 'T'; mp['U'] = 'U'; mp['V'] = 'V';
	mp['W'] = 'W'; mp['X'] = 'X'; mp['Y'] = 'Y'; mp['Z'] = '5';
	mp['1'] = '1'; mp['2'] = 'S'; mp['3'] = 'E'; mp['5'] = 'Z'; mp['8'] = '8';
}

int main()
{
	mirroredInit();
	while (cin >> before)
	{
		int len = strlen(before);
		reverse_copy(before, before + strlen(before), after);

		bool flagP = true, flagM = true; //是否回文,是否镜像
		for (int i = 0; i < len; i++) //判断是否回文
		{
			if (before[i] != after[i])
				flagP = false;
		}
		for (int i = 0; i < len; i++) //判断是否镜像
		{
			if (mp.find(after[i]) == mp.end())
				flagM = false;
			else if (before[i] != mp[after[i]])
				flagM = false;
		}

		cout << before;
		if (!flagP && !flagM)
			cout << " -- is not a palindrome." << endl;
		else if (flagP && !flagM)
			cout << " -- is a regular palindrome." << endl;
		else if (!flagP && flagM)
			cout << " -- is a mirrored string." << endl;
		else
			cout << " -- is a mirrored palindrome." << endl;
		cout << endl;

		memset(before, 0, sizeof(before));
		memset(after, 0, sizeof(after));
	}
	return 0;
}

继续加油。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值