紫书——Palindromes UVA - 401

题解:

改题目要记录下镜像的字母,然后判断是否相同(即使镜像转换后,字符串反转是否与原串相同),理解好题意就好

注意点:不是每个答案之间空一横,而是每输出一个就要输出一行,不然会PE

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

char has[] = "AEHIJLMOSTUVWXYZ12358";
char cha[] = "A3HILJMO2TUVWXY51SEZ8";

bool prime(string str) {
	int r = str.size()-1;
	int l = 0;
	while(l < r) {
		if(str[l] != str[r]) return false;
		l++; r--;
	}
	return true;
}

bool mirror(string str) {
	int lon = strlen(has);
	string tmp = str;
	int len = tmp.size();
	for(int i = 0; i < len; i++) {
		int pos = find(has,has+lon,str[i]) - has;
		if(pos == lon) return false;	//没有镜像的字符
		else tmp[i] = cha[pos];
	}
	for(int i = 0; i < len; i++) {
		if(str[i] != tmp[len-i-1]) return false;
	}
	return true;
}

int main() {
	//freopen("in.txt","r",stdin);
	string str;
	int fir = 0;
	while(getline(cin,str)) {
		bool pri, mir;
		pri = prime(str);
		mir = mirror(str);
		cout << str;
		if(!pri && !mir) printf(" -- is not a palindrome.\n");
		if(pri && !mir) printf(" -- is a regular palindrome.\n");
		if(!pri && mir) printf(" -- is a mirrored string.\n");
		if(pri && mir) printf(" -- is a mirrored palindrome.\n");
		fir = 1;
		printf("\n");
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/a673953508/article/details/80100626