UVa-401 Palindromes回文词
虽然是水题,但是容易错。参照了紫书的代码可以写的很简洁。主要还是注意常量数组的使用,能让代码变得简单许多
1 #include <iostream> 2 #include <cstdio> 3 #include <string.h> 4 #include <algorithm> 5 #include <stack> 6 #include <math.h> 7 #pragma warning ( disable : 4996 ) 8 9 using namespace std; 10 11 int Max( int a, int b ) { return a>b?a:b; } 12 int Min( int a, int b ) { return a>b?b:a; } 13 14 const int inf = 0x3f3f3f3f; 15 const int maxn = 1000 + 10; 16 const char T[100] = "A...3..HIL.JM.O...2TUVWXY51SE.Z..8."; 17 char test[200]; 18 19 char getM( char c ) 20 { 21 if (isalpha(c)) return T[c-'A']; 22 else return T[c-'0'+25]; 23 } 24 int main() 25 { 26 while ( ~scanf("%s", test) ) 27 { 28 int len = strlen(test); 29 bool isP = true, isM = true; 30 for ( int i = 0; i < (len+1) / 2; i++ ) 31 { 32 if ( test[i] != test[len-1-i] ) isP = false; 33 if ( getM(test[i]) != test[len-1-i] ) isM = false; 34 } 35 if ((!isP)&&(!isM)) printf( "%s -- is %s\n\n", test, "not a palindrome." ); 36 if (isP&&(!isM)) printf( "%s -- is %s\n\n", test, "a regular palindrome." ); 37 if ((!isP)&&isM) printf( "%s -- is %s\n\n", test, "a mirrored string." ); 38 if (isP&&isM) printf( "%s -- is %s\n\n", test, "a mirrored palindrome." ); 39 } 40 return 0; 41 }
什么时候能够不再这么懒惰