|
|
题目:
输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序。即输出125、126、145、146.
网上搜到很多代码,下面这个比较简练,但是看了好久还是没看懂,求大神指点一下思路或者给出自己的解法,我比较菜,网上搜到的代码注释不详,理解比较困难。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void display(const char *buf, const char *sub, const int len, int tab[], int depth, int distance)
{
int i;
if (len == depth)
{
for (i = 0; i < len; ++i)
{
printf("%d", tab);
}
printf("\n");
}
else
{
while (*buf)
{
if (*buf == *sub)
{
tab[depth] = distance + 1;
display(buf + 1, sub + 1, len, tab, depth + 1, distance + 1);
}
++distance;
++buf;
}
}
}
int main()
{
int tab[100];
const char *buf = "abdbcc";
const char *sub = "abc";
display(buf, sub, strlen(sub), tab, 0, 0);
system("pause");
return 0;
}
|
|