---
# System prepended metadata

title: 12987 - Let's build a hacker script
tags: [recursion]

---

# 12987 - Let's build a hacker script

>author: Utin
###### tags: `recursion`

---

## Brief
See the code below

## Solution 0

```c=
#include <stdio.h>
#include <string.h>

char ans[21][21], s[21], p[21]; //ans可以想成是string的陣列
int length_s, length_p;
int hoho = 0; //有輸出就設成1

void f(int index_s, int index_p);

int main(void) {
    scanf("%s", s);
    scanf("%s", p);
    length_s = strlen(s);
    length_p = strlen(p);
    f(0, 0);
    if (hoho == 0) printf("What the hack!?\n");
	return 0;
}

void f(int index_s, int index_p) {
    if (index_p == length_p - 1) {
        //把剩下的全部append到ans[index_p]
        int k = strlen(ans[index_p]);
        for (int i = k; i < length_s/* - index_s*/; i++) {
            ans[index_p][i] = s[/*index_s + (i - k)*/i];
        }

		/****Output****/
        int invalid = 0;
        //檢查有無符合的結果
        for (int i = 0; i < length_p; i++) {
            if (p[i] != '#') {
                if (strlen(ans[i]) > 1 || ans[i][0] != p[i]) {
                    invalid = 1;
                    break;
                }
            }
        }
        //若有符合的結果
        if (!invalid) {
            int head = 0; //判斷是否為輸出第一項
            for (int i = 0; i < length_p; i++) {
                if (p[i] == '#') {
                    if (head == 1) printf(" ");
                    printf("%s", ans[i]);
                    head = 1;
                    hoho = 1;
                }
            }
            printf("\n");
        }
		/**************/
    }
    else {
        for (int i = index_s; i < length_s - 1; i++) {
            //ans[index_p].append(s[i])
            int temp1 = strlen(ans[index_p]);
            ans[index_p][temp1] = s[i];
            //遞迴
            f(i + 1, index_p + 1);
            //將遞迴裡面的結果還原
            int temp2 = strlen(ans[index_p + 1]);
            for (int j = 0; j < temp2; j++) {
                ans[index_p + 1][j] = '\0';
            }
        }
    }
}
// By Utin
```

## Reference