刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 1(String)
第一题:401 - Palindromes
解题思路:此题很水,只要把 mirrored string 类的对应关系搞对,基本就可以了! 但是细节要注意,首先只有一个元素的时候需要单独判断,一个字符是回文串,是不是 mirrored string 则需要判断,另外最后输出中间隔了一行!
解题代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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.\n\n"; 49 else if (ms) 50 cout << ch << " -- is a mirrored string.\n\n"; 51 else if (ps) 52 cout << ch << " -- is a regular palindrome.\n\n"; 53 else cout << ch << " -- is not a palindrome.\n\n"; 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的地方找死循环的原因找了一下午,哎!还需使劲练习啊!
解题代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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] = '\0'; 61 getchar(); 62 } 63 /* 64 for (int i = 1; i <= n; i++) 65 { 66 for (int j = 1; j <= m; j ++) 67 printf("%c", map[i][j]); 68 printf("\n"); 69 } 70 */ 71 scanf ("%d", &k); 72 getchar(); 73 int len; 74 char ch[100]; 75 for (int i = 1; i <= k; i ++) 76 { 77 scanf ("%s", ch); 78 len = strlen(ch); 79 for (int j = 0; j < len; j ++) 80 { 81 if (ch[j] <= 'Z') 82 ch[j] = ch[j] - 'A' + 'a'; 83 } 84 strcpy(ser[i], ch); 85 ser[i][len] = '\0'; 86 } 87 /* 88 for (int i = 1; i <= k; i ++) 89 printf("len = %d %s\n", strlen(ser[i]), ser[i]); 90 */ 91 } 92 93 int main () 94 { 95 scanf ("%d", &T); 96 while (T--) 97 { 98 scanf ("%d%d", &n, &m); 99 getchar(); 100 init(); 101 for (q = 1; q <= k; q ++)//就是在这里使用了goto导致q一直为1 102 { 103 int tag = 0; 104 len = strlen(ser[q]); 105 char ch = ser[q][0]; 106 for (int i = 1; i <= n; i ++) 107 { 108 for (int j = 1; j <= m; j ++) 109 { 110 if (ch == map[i][j]) 111 if (serch(i, j, 1)) 112 { 113 cun[q] = (CUN){i, j}; 114 tag = 1; 115 } 116 if (tag) 117 break; 118 } 119 if (tag) 120 break; 121 } 122 } 123 for (int i = 1; i <= k; i ++) 124 printf ("%d %d\n", cun[i].x, cun[i].y); 125 if (T) 126 printf ("\n"); 127 } 128 return 0; 129 }
第三题:10361 - Automatic Poetry
UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1302
解题思路:水题一道!
解题代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // File Name :刘汝佳-基础/part2/10361 - Automatic Poetry 2 // Author :Freetion 3 // Created Time :2013年07月22日 星期一 20时10分53秒 4 5 #include <stdio.h> 6 #include <string> 7 #include <string.h> 8 #include <iostream> 9 //#define LOCAL 10 using namespace std; 11 12 const int max_l = 102; 13 char str1[max_l], str2[max_l], str3[max_l], str4[max_l], str5[max_l], str6[max_l]; 14 15 int main() 16 { 17 #ifdef LOCAL 18 freopen("data.in", "r", stdin); 19 freopen("data.out", "w", stdout); 20 #endif 21 int n, num1, num2, num3, num4; 22 while (~scanf("%d", &n)) 23 { 24 getchar(); 25 for (int i = 0; i < n; i ++) 26 { 27 gets(str1); 28 gets(str2); 29 int len1 = strlen(str1); 30 int tag = 0; 31 num1 = num2 = num3 = num4 = 0; 32 for (int j = 0; j < len1; j ++) 33 { 34 if (str1[j] != '<' && str1[j] != '>') 35 printf ("%c", str1[j]); 36 else if (str1[j] == '<') 37 tag ++; 38 else if (str1[j] == '>') 39 tag ++; 40 if (tag == 1) 41 str3[num1++] = str1[j]; 42 else if (tag == 2) 43 str5[num3++] = str1[j]; 44 else if (tag == 3) 45 str4[num2++] = str1[j]; 46 else if (tag == 4) 47 str6[num4++] = str1[j]; 48 49 } 50 printf("\n"); 51 int len2 = strlen(str2); 52 for (int j = 0; j < len2-3; j ++) 53 printf ("%c", str2[j]); 54 for (int j = 1; j < num2; j ++) 55 printf("%c", str4[j]); 56 for (int j = 1; j < num3; j ++) 57 printf ("%c", str5[j]); 58 for (int j = 1; j < num1; j ++) 59 printf ("%c", str3[j]); 60 for (int j = 1; j < num4; j ++) 61 printf ("%c", str6[j]); 62 printf ("\n"); 63 64 } 65 66 } 67 return 0; 68 }
第四题:537 - Artificial Intelligence?
UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=478
解题思路:按关键字将数据提取出来即可,水题!还有,输出是每个案列后都会有一个空行,这里wa三次! UVA为什么没有PE呢?
解题代码:好吧,可能太长,有点凌乱!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // File Name :刘汝佳-基础/part2/537 - Artificial Intelligence? 2 // Author :Freetion 3 // Created Time :2013年07月22日 星期一 22时25分10秒 4 5 #include <stdio.h> 6 #include <string.h> 7 8 const int max_l = 1000; 9 char phy[max_l]; 10 11 int main() 12 { 13 int t, T; 14 scanf ("%d", &t); 15 T = t; 16 getchar(); 17 double U, I, P; 18 int tag_u = 0, tag_i = 0, tag_p = 0; 19 while(t--) 20 { 21 gets(phy); 22 int len = strlen(phy); 23 U = I = P = 0; 24 for (int i = 0; i < len; i ++) 25 { 26 if (phy[i] == 'U' && phy[++i] == '=') 27 { 28 tag_u = 1; 29 int tag = 1; 30 int tm; 31 i ++; 32 while(phy[i] == '.' || (phy[i] >= '0' && phy[i] <= '9')) 33 { 34 if (phy[i] == '.') 35 { 36 tm = 10; 37 tag = 0; 38 i ++; 39 } 40 if (tag) 41 U = U*10 + phy[i] - '0'; 42 else 43 { 44 U = U + (phy[i] - '0')*1.0/tm; 45 tm *= 10; 46 } 47 i ++; 48 } 49 if (phy[i] == 'm') 50 U = U/1000; 51 else if (phy[i] == 'k') 52 U = U*1000; 53 else if (phy[i] == 'M') 54 U = U*1000*1000; 55 } 56 if (phy[i] == 'I' && phy[++i] == '=') 57 { 58 tag_i = 1; 59 int tag = 1; 60 int tm; 61 i ++; 62 while(phy[i] == '.' || (phy[i] >= '0' && phy[i] <= '9')) 63 { 64 if (phy[i] == '.') 65 { 66 tm = 10; 67 tag = 0; 68 i ++; 69 } 70 if (tag) 71 I = I*10 + phy[i] - '0'; 72 else 73 { 74 I = I + (phy[i] - '0')*1.0/tm; 75 tm *= 10; 76 } 77 i ++; 78 } 79 if (phy[i] == 'm') 80 I = I/1000; 81 else if (phy[i] == 'k') 82 I = I*1000; 83 else if (phy[i] == 'M') 84 I = I*1000*1000; 85 } 86 if (phy[i] == 'P' && phy[++i] == '=') 87 { 88 tag_p = 1; 89 int tag = 1; 90 int tm; 91 i ++; 92 while(phy[i] == '.' || (phy[i] >= '0' && phy[i] <= '9')) 93 { 94 if (phy[i] == '.') 95 { 96 tm = 10; 97 tag = 0; 98 i ++; 99 } 100 if (tag) 101 P = P*10 + phy[i] - '0'; 102 else 103 { 104 P = P + (phy[i] - '0')*1.0/tm; 105 tm *= 10; 106 } 107 i ++; 108 } 109 if (phy[i] == 'm') 110 P = P/1000; 111 else if (phy[i] == 'k') 112 P = P*1000; 113 else if (phy[i] == 'M') 114 P = P*1000*1000; 115 } 116 if (tag_p && tag_u) 117 break; 118 if (tag_u && tag_i) 119 break; 120 if (tag_p && tag_i) 121 break; 122 123 } 124 printf ("Problem #%d\n", T - t); 125 if (tag_u && tag_i) 126 { 127 tag_u = tag_i = 0; 128 printf ("P=%.2fW\n", U*I); 129 } 130 else if (tag_u && tag_p) 131 { 132 tag_u = tag_p = 0; 133 printf("I=%.2fA\n", P/U); 134 } 135 else 136 { 137 printf("U=%.2fV\n", P/I); 138 tag_i = tag_p = 0; 139 } 140 //if (t) 141 printf ("\n"); 142 } 143 return 0; 144 }
第五题:409 - Excuses, Excuses!
解题思路:暴力搜素即可,但须注意判断条件。
解题代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <stdio.h> 3 #include <map> 4 #include <algorithm> 5 #include <string.h> 6 using namespace std; 7 //#define LOCAL 8 9 struct SENT 10 { 11 int sign, num; 12 bool operator < (const SENT s) const 13 { 14 if (num == s.num) 15 return sign < s.sign; 16 return num > s.num; 17 } 18 }sen[23]; 19 20 int main() 21 { 22 #ifdef LOCAL 23 freopen("data.in", "r", stdin); 24 freopen("data.out", "w", stdout); 25 #endif 26 char key[23][23], sent[23][1000]; 27 int n, m, T = 0; 28 while (~scanf("%d%d", &n, &m)) 29 { 30 getchar(); 31 for (int i = 0; i < n; i++) 32 { 33 scanf ("%s", key[i]); 34 getchar(); 35 int len = strlen(key[i]); 36 for (int j = 0; j < len; j ++) 37 { 38 if (key[i][j] <= 'Z') 39 key[i][j] = key[i][j] - 'A' + 'a'; 40 } 41 } 42 bool tag[23]; 43 for (int i = 0; i < m; i ++) 44 { 45 memset(tag, true, sizeof(tag)); 46 sen[i].num = 0; 47 sen[i].sign = i; 48 gets(sent[i]); 49 // puts(sent[i]); 50 int len = strlen(sent[i]); 51 for (int j = 0; j < len; j ++) 52 { 53 if (j == 0 || ((sent[i][j-1] > 'Z' || sent[i][j-1] < 'A') && (sent[i][j-1] < 'a' || sent[i][j-1] > 'z'))) 54 {//不能仅用空格作为判断条件 55 for (int k = 0; k < n; k ++) 56 { 57 if ((key[k][0] == sent[i][j] || key[k][0] == sent[i][j]-'A'+'a') && tag[k]) 58 { 59 int len_k; 60 int l; 61 len_k = strlen(key[k]); 62 for (l = 1; l < len_k; l ++) 63 { 64 if (j+l < len && key[k][l] != sent[i][j+l] && key[k][l] != sent[i][j+l]-'A'+'a') 65 break; 66 } 67 if (l == len_k) 68 { 69 tag[k] = false; 70 sen[i].num ++; 71 j = j+l; 72 } 73 } 74 } 75 } 76 } 77 } 78 sort(sen, sen+m); 79 printf("Excuse Set #%d\n", ++T); 80 for (int i = 0; i < m; i ++) 81 if (sen[i].num == sen[0].num) 82 puts(sent[sen[i].sign]); 83 puts(""); 84 } 85 return 0; 86 }
第六题:10878 - Decode the tape
解题思路:找出输入字符与ASCII码的关系就可输出,这里以output打印出 A 作介绍:对应的字符串:
| | o | . | o | | | ||||||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
这里列出打印 A 字符的依据,并标上列数, A 字符的ASCII码值为 65 而 65 = 2(10 - 4) + 2(10 - 10) 所以打印 A字符,其余字符也是如此计算!
解题代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 using namespace std; 5 //#define LOCAL 6 int main() 7 { 8 #ifdef LOCAL 9 freopen("data.in", "r", stdin); 10 freopen("data.out", "w", stdout); 11 #endif 12 char ch; 13 int tag = 0; 14 while (1) 15 { 16 int s = 0; 17 for (int i = 0; i < 11; i++) 18 { 19 scanf ("%c", &ch); 20 if (ch == '_') 21 tag ++; 22 if (ch == 'o') 23 { 24 if (i < 6) 25 s += pow(2, 8-i); 26 else s += pow(2, 9 - i); 27 } 28 } 29 getchar(); 30 if (s) 31 printf("%c", s); 32 if (tag == 22) 33 break; 34 } 35 }
第七题:10815 - Andy's First Dictionary
uva:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1756
解题思路:输入字符串,找出所有单词,找过过程中,判断是否保存过,保存过就抛弃,没有则保存,并标记已保存,然后排序输出就可。
解题代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <string.h> 6 #include <string> 7 #include <map> 8 using namespace std; 9 10 #define LOCAL 11 12 #ifdef WINDOWS 13 #define LL __int64 14 #define LLD "%I64d" 15 #else 16 #define LL long long 17 #define LLD "%lld" 18 #endif 19 20 int main() 21 { 22 23 #ifdef LOCAL 24 freopen("data.in", "r", stdin); 25 freopen("data.out", "w", stdout); 26 #endif 27 28 map <string, int> word; 29 string str[10000], tm1_str, tm2_str; 30 int cun = 0; 31 while (cin >> tm1_str) 32 { 33 int len = tm1_str.length(); 34 35 for (int i = 0; i < len; i ++) 36 { 37 tm2_str.clear(); 38 while((tm1_str[i] <= 'Z' && tm1_str[i] >= 'A') || (tm1_str[i] <= 'z' && tm1_str[i] >= 'a')) 39 { 40 if (tm1_str[i] <= 'Z') 41 tm2_str += (tm1_str[i] - 'A' + 'a'); 42 else tm2_str += tm1_str[i]; 43 i ++; 44 if (i >= len) 45 break; 46 } 47 if (!tm2_str.empty()) 48 { 49 if (word[tm2_str] == 0) 50 { 51 str[cun++] = tm2_str; 52 word[tm2_str] = 1; 53 } 54 } 55 } 56 57 } 58 sort(str, str+cun); 59 for (int i = 0; i < cun; i ++) 60 cout << str[i] << endl; 61 return 0; 62 }
第八题:644 - Immediate Decodability
UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=585
解题思路:先按字典序或字符串长短排个序,然后就挨个找就可以了,字典序也可以是因为比如0000,01后者肯定不是前者的前缀,至于00, 000这一类就需要进行判断!
解题代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <string.h> 6 #include <string> 7 #include <map> 8 using namespace std; 9 10 //#define LOCAL //Please annotate this line when you submit 11 12 #ifdef WINDOWS 13 #define LL __int64 14 #define LLD "%I64d" 15 #else 16 #define LL long long 17 #define LLD "%lld" 18 #endif 19 20 string str, str1[1000]; 21 map <string, bool> IM; 22 23 int solve(int cun) 24 { 25 int tag = 1; 26 sort(str1, str1+cun); 27 for (int i = 0; i < cun && tag; i ++) 28 { 29 string temp; 30 temp.clear(); 31 int len = str1[i].length(); 32 for (int j = 0; j < len && tag; j ++) 33 { 34 temp += str1[i][j]; 35 if (IM[temp]) 36 { 37 tag = 0; 38 break; 39 } 40 } 41 IM[str1[i]] = 1; 42 str1[i].clear(); 43 } 44 return tag; 45 } 46 47 int main() 48 { 49 50 #ifdef LOCAL 51 freopen("data.in", "r", stdin); 52 freopen("data.out", "w", stdout); 53 #endif 54 55 int T = 0; 56 bool tag = 1; 57 int cun = 0; 58 while (cin>>str) 59 { 60 if (str[0] == '9') 61 { 62 tag = solve(cun); 63 if (tag) 64 printf("Set %d is immediately decodable\n", ++T); 65 else printf("Set %d is not immediately decodable\n", ++T); 66 tag = 1; 67 IM.clear(); 68 cun = 0; 69 } 70 else 71 str1[cun ++] = str; 72 } 73 return 0; 74 }
第九题:10115 - Automatic Editing
UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1056
解题思路:字符串匹配与替换,水题
解题代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <string.h> 6 #include <string> 7 #include <map> 8 using namespace std; 9 10 //#define LOCAL //Please annotate this line when you submit 11 12 #ifdef WINDOWS 13 #define LL __int64 14 #define LLD "%I64d" 15 #else 16 #define LL long long 17 #define LLD "%lld" 18 #endif 19 20 const int max_n = 1000; 21 22 int n; 23 char rep[max_n][max_n], by[max_n][max_n]; 24 char str[2][max_n]; 25 26 int init() 27 { 28 if(~scanf ("%d", &n) && n) 29 { 30 getchar(); 31 for (int i = 0; i < n; i++) 32 { 33 gets(rep[i]); 34 gets(by[i]); 35 } 36 gets(str[0]); 37 // puts(str[0]); 38 return 1; 39 } 40 return 0; 41 } 42 43 int solve() 44 { 45 int pos = 0; 46 for (int i = 0; i < n; i ++) 47 { 48 int len = strlen(str[pos]); 49 for(int j = 0; j < len; j ++) 50 { 51 if(str[pos][j] == rep[i][0]) 52 { 53 int k; 54 int tm_len = strlen(rep[i]); 55 for (k = 1; k < tm_len && j+k < len; k ++) 56 { 57 if (str[pos][j+k] != rep[i][k]) 58 break; 59 } 60 if (tm_len == k) 61 { 62 int cun = 0; 63 for (int r = 0; r < len; r ++) 64 { 65 if (r != j) 66 str[pos^1][cun++] = str[pos][r]; 67 else 68 { 69 int by_len = strlen(by[i]); 70 for(int l = 0; l < by_len; l ++) 71 str[pos^1][cun++] = by[i][l]; 72 r = j + tm_len - 1; 73 } 74 } 75 str[pos^1][cun] = '\0'; 76 j = 0; 77 len = strlen(str[pos^1]); 78 pos ^= 1; 79 } 80 } 81 } 82 } 83 return pos; 84 } 85 86 int main() 87 { 88 89 #ifdef LOCAL 90 freopen("data.in", "r", stdin); 91 freopen("data.out", "w", stdout); 92 #endif 93 while(init()) 94 { 95 int pos = solve(); 96 puts(str[pos]); 97 } 98 return 0; 99 }