UVA401(回文串好题)
2014-11-25 19:43
169 查看
Palindromes
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19520
Description
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.
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.
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.
Hint
use the C++'s class of string will be convenient, but not a must
解题思路:
这道题蛮好的···WA了我一天。题意是说给你有两种串,一种是普通回文串,另一种是Reverse后与原串相同。
判断回文串这个好弄。在考虑最多的是判断另一种串。题目描述里说如果该串可以Reverse的话那么Reverse作比较,否则直接可以判定它不是mirrored string。
完整代码:
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19520
Description
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 |
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 |
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.
Hint
use the C++'s class of string will be convenient, but not a must
解题思路:
这道题蛮好的···WA了我一天。题意是说给你有两种串,一种是普通回文串,另一种是Reverse后与原串相同。
判断回文串这个好弄。在考虑最多的是判断另一种串。题目描述里说如果该串可以Reverse的话那么Reverse作比较,否则直接可以判定它不是mirrored string。
完整代码:
#include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <numeric> #include <cstring> #include <climits> #include <cassert> #include <complex> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") typedef long long LL; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; /** Constant List .. **/ //{ const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const DB PI = acos(-1.0); //M_PI; const int maxn = 1001; char t[maxn]; //char t1[maxn] , t2[maxn]; bool check1(char t[] , int len) { for(int i = 0 , j = len - 1 ; i < j ; i ++ , j --) if(t[i] != t[j]) return false; return true; } bool check2(char t[] , int len , char t1[] , char t2[]) { char s[maxn]; memset(s , 0 ,sizeof(s)); int is = 0; for(int i = 0 ; i < len ; i ++) { int flag = 0; for(int j = 0 ; j < 21 ;j ++) { if(t[i] == t1[j]) { s[i] = t2[j]; flag = 1; break; } } if(flag == 0) { s[i] = t[i]; is = 1; } } if(is) return false; for(int i = 0 , j = len - 1 ; i < len && j >= 0 ; i ++ , j--) if(s[i] != t[j]) return false; return true; } int main() { #ifdef DoubleQ freopen("in.txt","r",stdin); #endif char t1[] = {'A' , 'E' , 'H' , 'I' , 'J' , 'L' , 'M' , 'O' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , '1' , '2' , '3' , '5' , '8'}; char t2[] = {'A' , '3' , 'H' , 'I' , 'L' , 'J' , 'M' , 'O' , '2' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , '5' , '1' , 'S' , 'E' , 'Z' , '8'}; while(~scanf("%s",t)) { string str(t); int len = strlen(t); int flag1 = 0 , flag2 = 0; if(check1(t , len)) flag1 = 1; if(check2(t , len , t1 , t2)) flag2 = 1; if(flag1 && flag2) cout << str << " -- is a mirrored palindrome." << "\n\n"; else if(flag1 && !flag2) cout << str << " -- is a regular palindrome." << "\n\n"; else if(!flag1 && flag2) cout << str << " -- is a mirrored string." << "\n\n"; else if(!flag1 && !flag2) cout << str << " -- is not a palindrome." << "\n\n"; } }
相关文章推荐
- 回文串和镜像串,组合不同,下标为何*2 。 UVA 401 Palindromes 回文词
- UVA 401 Palindromes 回文串和镜像串
- UVa 401 Palindromes (回文串与镜像串的综合判断)
- UVA 401 - Palindromes(判断回文串和镜像串)
- uva401(回文串)
- uva401 Palindromes(回文串判断 水题)
- UVA - 401 Palindromes (回文串)
- UVA 401
- UVa401
- UVA 401 - Palindromes
- UVa 401 Palindromes
- uva10453(回文串dp)
- UVa 401 Palindromes(常量数组+遍历)
- UVA 11584 Partitioning by Palindromes 划分成回文串(DP + 预处理)
- UVA 11151 Longest Palindrome (最大回文串)
- Uva11584 最少回文串
- UVA 401 Palindromes
- UVa 401 Palindromes
- UVa 401 - Palindromes
- uva 401