输出包含两行,第一行包含一个字符串代表str,第二行包含一个字符串,代表strips。
输出一行,代表返回的值。
A1B21C 121
AC1B2B1CA
str=“A1B21C",strlps="121",返回“AC1B2B1CA”或者“CA1B2B1AC”,总之,只要是添加的字符数最少,只返回其中一种结果即可。
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXLEN 5001 int main(void) { char str[MAXLEN], strips[MAXLEN], *res; scanf("%s%s", str, strips); int n = strlen(str); int m = strlen(strips); res = (char *) malloc(2 * n - m + 1); res[2 * n - m] = '\0'; int l = 0, r = 2 * n - m - 1; int ll = 0, lr = 0, rl = n - 1, rr = n - 1; int i = 0, j = m - 1; while (i <= j) { while (str[lr] != strips[i]) lr++; while (str[rl] != strips[j]) rl--; while (ll < lr) { res[l++] = str[ll]; res[r--] = str[ll++]; } while (rl < rr) { res[l++] = str[rr]; res[r--] = str[rr--]; } ll = ++lr; rr = --rl; res[l++] = strips[i++]; res[r--] = strips[j--]; } printf("%s\n", res); free(res); return 0; }