题目描述:https://pycoder.blog.csdn.net/article/details/125703694
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *name_word[10] = {0};
char dstName[10] = {0};
int dfs(int word_len, int word_index, int dstNameLen, int dstNameIndex)
{
int ret;
if ((dstNameIndex == dstNameLen) || (word_index == word_len)) {
if ((dstNameIndex == dstNameLen) && (word_index == word_len)) {
return 1;
} else {
return 0;
}
}
for (int i = 0 ; i < strlen(name_word[word_index]); i++) {
if (name_word[word_index][i] == dstName[dstNameIndex]) {
word_index++;
dstNameIndex++;
ret = dfs(word_len, word_index, dstNameLen, dstNameIndex);
if (ret != 0) return ret;
word_index--;
} else {
break;
}
}
return 0;
}
int main(void)
{
char str[1000] = {0};
char *name[100] = {0};
char *tmp;
char name_tmp[100] = {0};
int index = 1;
int word_index;
int dst_index = 0;
int dst_len;
char res_name[10][50] = {0};
int res_index = 0;
gets(str);
scanf("%s", dstName);
dst_len = strlen(dstName);
name[0] = strtok(str, ",");
while (1) {
tmp = strtok(NULL, ",");
if (tmp == NULL) break;
name[index++] = tmp;
}
for (int i = 0; i < index; i++) {
strcpy(name_tmp, name[i]);
word_index = 1;
name_word[0] = strtok(name[i], " ");
while (1) {
tmp = strtok(NULL, " ");
if (tmp == NULL) break;
name_word[word_index++] = tmp;
}
if (dfs(word_index, 0, dst_len, 0)) {
strcpy(res_name[res_index++], name_tmp);
}
}
for (int i = 0; i < res_index - 1; i++) {
printf("%s,", res_name[i]);
}
printf("%s\n", res_name[res_index - 1]);
return 0;
}