第一题:401 - Palindromes
解题思路:此题很水,只要把 mirrored string 类的对应关系搞对,基本就可以了! 但是细节要注意,首先只有一个元素的时候需要单独判断,一个字符是回文串,是不是 mirrored string 则需要判断,另外最后输出中间隔了一行!
解题代码:
1 // File Name: 刘汝佳-基础/part2/Palindromes/401.cpp 2 // Author: sheng 3 // Created Time: 2013年07月20日 星期六 23时37分45秒 4 5 #include <iostream> 6 #include <stdio.h> 7 #include <string.h> 8 using namespace std; 9 10 const int max_n = 100000; 11 const int max_f = 100; 12 13 int main () 14 { 15 bool mir[max_f][max_f]; 16 char ch[max_n]; 17 memset (mir, false, sizeof (mir)); 18 mir['A']['A'] = true; 19 mir['E']['3'] = mir['3']['E'] = true; 20 mir['H']['H'] = true; 21 mir['I']['I'] = 1; 22 mir['J']['L'] = mir['L']['J'] = 1; 23 mir['M']['M'] = 1; 24 mir['O']['O'] = 1; 25 mir['S']['2'] = mir['2']['S'] = 1; 26 for (int i = 'T'; i <= 'Y'; i ++) 27 mir[i][i] = 1; 28 mir['Z']['5'] = mir['5']['Z'] = 1; 29 mir['1']['1'] = 1; 30 mir['8']['8'] = 1; 31 while (~scanf ("%s", ch)) 32 { 33 bool ms = true, ps = true; 34 int len = strlen (ch); 35 // cout << len << endl; 36 if (len == 1 && (!mir[ch[0]][ch[0]])) 37 ms = false; 38 for (int i = 0; i < len/2; i ++) 39 { 40 if ( ch[i] != ch[len-1-i] ) 41 ps = false; 42 if (!mir[ch[i]][ch[len-1-i]]) 43 ms = false; 44 if ((!ms) && (!ps)) 45 break; 46 } 47 if (ms && ps) 48 cout << ch << " -- is a mirrored palindrome. "; 49 else if (ms) 50 cout << ch << " -- is a mirrored string. "; 51 else if (ps) 52 cout << ch << " -- is a regular palindrome. "; 53 else cout << ch << " -- is not a palindrome. "; 54 } 55 return 0; 56 }
第二题:10010 - Where's Waldorf?
UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=951
解题思路:因为数据范围很小所以可以暴力搜索,总共八个方向,只要有一个方向可以组成当前单词就标记此开始点并跳出,寻找下一个单词!
水题一道,却花了一下午的时间,英语差是一方面,最不该就是使用了goto,害得程序陷入死循环,而我却在没有bug的地方找死循环的原因找了一下午,哎!还需使劲练习啊!
解题代码:
1 // File Name :刘汝佳-基础/part2/10010 - Where's Waldorf? 2 // Author :Freetion 3 // Created Time :2013年07月22日 星期一 14时32分18秒 4 5 #include <string.h> 6 #include <stdio.h> 7 8 char map[52][52]; 9 bool tag[52][52]; 10 int T; 11 int n, m, k, q; 12 char ser[23][100]; 13 int len; 14 15 struct CUN 16 { 17 int x, y; 18 }cun[23]; 19 20 const int alt_x[8] = {-1, -1, 1, 1, 0, 0, -1, 1}; 21 const int alt_y[8] = {-1, 1, -1, 1, -1, 1, 0, 0}; 22 23 int serch(int x, int y, int l) 24 { 25 for (int i = 0; i < 8; i ++)//八个方向 26 { 27 int xx = x + alt_x[i]; 28 int yy = y + alt_y[i]; 29 l = 1; 30 while (l < len && xx > 0 && xx <= n && yy > 0 && yy <= m)//向一个方向找 31 { 32 int temp_x = xx + alt_x[i]; 33 int temp_y = yy + alt_y[i]; 34 if (ser[q][l] == map[xx][yy]) 35 { 36 l ++; 37 xx = temp_x; 38 yy = temp_y; 39 } 40 else break; 41 } 42 if (l == len) 43 return 1;//找到返回1 44 } 45 return 0;//没有返回0 46 } 47 48 void init() 49 { 50 for (int i = 1; i <= n; i ++) 51 { 52 for (int j = 1; j <= m; j ++) 53 { 54 char ch; 55 scanf ("%c", &ch); 56 if (ch <= 'Z') 57 ch = ch - 'A' + 'a'; 58 map[i][j] = ch; 59 } 60 map[i][m+1] = '