题目:输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。
例如,输入”They are students.”和”aeiou”,
例如,输入”They are students.”和”aeiou”,
则删除之后的第一个字符串变成”Thy r stdnts.”。
解法一: O(N+M)
#include <stdio.h>
#define TRANS_ARR_MAX 128
#define ARR_MAX 50
void TransArrInit(int ua_trans_arr_[]) {
ua_trans_arr_[(int)'0'] = 1;
ua_trans_arr_[(int)'1'] = 2;
ua_trans_arr_[(int)'2'] = 3;
ua_trans_arr_[(int)'3'] = 4;
ua_trans_arr_[(int)'4'] = 5;
ua_trans_arr_[(int)'5'] = 6;
ua_trans_arr_[(int)'6'] = 7;
ua_trans_arr_[(int)'7'] = 8;
ua_trans_arr_[(int)'8'] = 9;
ua_trans_arr_[(int)'9'] = 10;
ua_trans_arr_[(int)'a'] = 11;
ua_trans_arr_[(int)'b'] = 12;
ua_trans_arr_[(int)'c'] = 13;
ua_trans_arr_[(int)'d'] = 14;
ua_trans_arr_[(int)'e'] = 15;
ua_trans_arr_[(int)'f'] = 16;
ua_trans_arr_[(int)'g'] = 17;
ua_trans_arr_[(int)'h'] = 18;
ua_trans_arr_[(int)'i'] = 19;
ua_trans_arr_[(int)'j'] = 20;
ua_trans_arr_[(int)'k'] = 21;
ua_trans_arr_[(int)'l'] = 22;
ua_trans_arr_[(int)'m'] = 23;
ua_trans_arr_[(int)'n'] = 24;
ua_trans_arr_[(int)'o'] = 25;
ua_trans_arr_[(int)'p'] = 26;
ua_trans_arr_[(int)'q'] = 27;
ua_trans_arr_[(int)'r'] = 28;
ua_trans_arr_[(int)'s'] = 29;
ua_trans_arr_[(int)'t'] = 30;
ua_trans_arr_[(int)'u'] = 31;
ua_trans_arr_[(int)'v'] = 32;
ua_trans_arr_[(int)'w'] = 33;
ua_trans_arr_[(int)'x'] = 34;
ua_trans_arr_[(int)'y'] = 35;
ua_trans_arr_[(int)'z'] = 36;
ua_trans_arr_[(int)'A'] = 11;
ua_trans_arr_[(int)'B'] = 12;
ua_trans_arr_[(int)'C'] = 13;
ua_trans_arr_[(int)'D'] = 14;
ua_trans_arr_[(int)'E'] = 15;
ua_trans_arr_[(int)'F'] = 16;
ua_trans_arr_[(int)'G'] = 17;
ua_trans_arr_[(int)'H'] = 18;
ua_trans_arr_[(int)'I'] = 19;
ua_trans_arr_[(int)'J'] = 20;
ua_trans_arr_[(int)'K'] = 21;
ua_trans_arr_[(int)'L'] = 22;
ua_trans_arr_[(int)'M'] = 23;
ua_trans_arr_[(int)'N'] = 24;
ua_trans_arr_[(int)'O'] = 25;
ua_trans_arr_[(int)'P'] = 26;
ua_trans_arr_[(int)'Q'] = 27;
ua_trans_arr_[(int)'R'] = 28;
ua_trans_arr_[(int)'S'] = 29;
ua_trans_arr_[(int)'T'] = 30;
ua_trans_arr_[(int)'U'] = 31;
ua_trans_arr_[(int)'V'] = 32;
ua_trans_arr_[(int)'W'] = 33;
ua_trans_arr_[(int)'X'] = 34;
ua_trans_arr_[(int)'Y'] = 35;
ua_trans_arr_[(int)'Z'] = 36;
ua_trans_arr_[(int)'*'] = 37;
ua_trans_arr_[(int)'.'] = 38;
ua_trans_arr_[(int)'#'] = 39;
ua_trans_arr_[(int)' '] = 39;
ua_trans_arr_[(int)';'] = 40;
ua_trans_arr_[(int)'('] = 41;
ua_trans_arr_[(int)')'] = 42;
ua_trans_arr_[(int)'/'] = 43;
ua_trans_arr_[(int)'-'] = 44;
ua_trans_arr_[(int)'_'] = 45;
ua_trans_arr_[(int)'.'] = 46;
}
int main() {
char* str1 = "They are students.";
char* str2 = "aeiou";
char* str1_start = str1;
int ua_trans_arr_[TRANS_ARR_MAX] = {0};
int map_arr[ARR_MAX] = {0};
TransArrInit(ua_trans_arr_); //初始化转码数组,如果区分大小写字母需要修改初始化
while (*str1_start != '\0') { //转码数组置位
map_arr[ua_trans_arr_[(int)*str1_start]] = 1;
++str1_start;
}
while (*str2 != '\0') { //有被置过位的清除掉
if (1 == map_arr[ua_trans_arr_[(int)*str2]]) {
map_arr[ua_trans_arr_[(int)*str2]] = 0;
}
++str2;
}
while (*str1 != '\0') { //没有被清除掉的输出
if (1 == map_arr[ua_trans_arr_[(int)*str1]]) {
printf("%c", *str1);
}
++str1;
}
printf("\n");
return 0;
}
解法二: O(N*M)
#include <stdio.h>
int main() {
char* str1 = "They are students.";
char* str2 = "aeiou";
char* str2_start = str2;
int exist_flag = 0;
while (*str1 != '\0') {
while (*str2_start != '\0') {
if (0 == (*str2_start ^ *str1)) { //str1当前字符在str2中出现过
exist_flag = 1;
break;
}
++str2_start;
}
if (0 == exist_flag) {
printf("%c", *str1);
}
++str1;
exist_flag = 0;
str2_start = str2;
}
printf("\n");
return 0;
}