高分求教,典型递归问题……

babaluoshahao 2012-05-28 08:20:44
程序员面试宝典面试例题:输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序,即输出125,126,145,146
请哪位能解释下该题的递归解法的思路,最好能将其思想用图的方式,讲解下……
...全文
334 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
ww884203 2012-06-02
  • 打赏
  • 举报
回复
18L的代码写得很好了,我把我自己写的也贴出来参考一下

#include <stdio.h>
#include <string.h>

char * s1;
char * s2;
int digitsave[20];

void output()
{
int length = strlen(s2);
for(int iter = 0 ; iter != length ;++iter)
{
printf("%d",digitsave[iter]);
}
printf("\n");
}

void cmp(int index1 , int index2)
{
if(s2[index2] == '\0')
{
output();
return;
}
if(s1[index1] == '\0') {return;}
if(s1[index1] != s2[index2])
{
cmp(index1+1 , index2);
return;
}
if(s1[index1] == s2[index2])
{
digitsave[index2] = index1+1;
cmp(index1+1,index2+1);
cmp(index1+1,index2);
}
}

int main(int argc , char ** argv)
{
s1 = argv[1];
s2 = argv[2];
cmp(0 , 0);
return(0);
}

运行结果:

ii@ubuntu:~/桌面/codes/tests$ ./a.out abdbcc abc
125
126
145
146
ii@ubuntu:~/桌面/codes/tests$ ./a.out adbbcbc abc
135
137
145
147
167
ii@ubuntu:~/桌面/codes/tests$ ./a.out abcdefghi aeh
158
w346581442 2012-06-02
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char s1[]="abdbcbc";
char s2[]="abc";
int i,*pos;

void find(int si,int di)
{
if(s2[di]==0)
{
for(i=0;i<3;++i) printf("%d",pos[i]+1);
printf("\n");
return;
}
while(s1[si]!=0)
{
if(s1[si]==s2[di])
{
pos[di]=si;
find(si+1,di+1);
}
++si;
}
};

void main()
{
pos=(int*)malloc(sizeof(int)*strlen(s2));
find(0,0);
free(pos);
}
w346581442 2012-06-02
  • 打赏
  • 举报
回复
我想用不着递归,用第二个串中的字符锁定在第一个串中的多个位置,然后就排列组合了
chenhao0568chen 2012-06-02
  • 打赏
  • 举报
回复
//以aabc abc为例

#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[i]);
}
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[100] ;
const char sub[100] ;
printf("请输入两个字符串(以空格分开,以回车结束):");
scanf("%s %s",buf,sub);
display(buf, sub, strlen(sub), tab, 0, 0);

system("pause");
return 0;
}
chenhao0568chen 2012-06-02
  • 打赏
  • 举报
回复
深度优先遍历 来自18楼

//以aabc abc为例

#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[i]);
}
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[100] ;
const char sub[100] ;
printf("请输入两个字符串(以空格分开,以回车结束):");
scanf("%s %s",buf,sub);
display(buf, sub, strlen(sub), tab, 0, 0);

system("pause");
return 0;
}

sxldfang 2012-05-29
  • 打赏
  • 举报
回复

//显示杨辉三角形
#include<stdio.h>

char s1[]="abdbcbc";
char s2[]="abc";
int i,*pos;

void find(int si,int di)
{
if(s2[di]==0)
{
for(i=0;i<3;++i) printf("%d",pos[i]+1);
printf("\n");
return;
}
while(s1[si]!=0)
{
if(s1[si]==s2[di])
{
pos[di]=si;
find(si+1,di+1);
}
++si;
}
}

void main()
{
pos=malloc(sizeof(int)*strlen(s2));
find(0,0);
free(pos);
}

运行结果:
125
127
145
147
167
请按任意键继续. . .


chenhao0568chen 2012-05-29
  • 打赏
  • 举报
回复
1. 从第二个字符串中得到第n个字符
2. 从第一个字符串中查找第n个字符 并记下位置号输出,回到第一步。
chenhao0568chen 2012-05-29
  • 打赏
  • 举报
回复
1. 从第二个字符串中得到第n个字符
2. 从第一个字符串中查找第n个字符 并记下位置号输出,回到第一步。
chenhao0568chen 2012-05-29
  • 打赏
  • 举报
回复
1. 从第二个字符串中得到第n个字符
2. 从第一个字符串中查找第n个字符, 并记下位置号输出,回到第一步。
fragileeye 2012-05-29
  • 打赏
  • 举报
回复
深度遍历就行吧。。
cobras 2012-05-29
  • 打赏
  • 举报
回复
#include <stdio.h>

int print_string_sequence(char buf[10], int l, const char *s1, int i1, const char *s2, int i2)
{
int i;
int cnt;

cnt = 0;
for (i = i1; s1[i] != '\0'; ++i) {
if (s1[i] == s2[i2]) {
cnt++;
buf[l] = i;
print_string_sequence(buf, l + 1, s1, i + 1, s2, i2 + 1);
}
}
if (!cnt) {
for (i = 0; i < l; ++i) {
printf("%d", buf[i] + 1);
}
printf("\n");
}
return 0;
}

int main(void)
{
char buf[10];
print_string_sequence(buf, 0, "abdbcc", 0, "abc", 0);
return 0;
}
babaluoshahao 2012-05-29
  • 打赏
  • 举报
回复
不给力啊……怎么没个高手出现呢…… 自己顶一个……
hello_world000 2012-05-29
  • 打赏
  • 举报
回复

#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[i]);
}
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;
}
babaluoshahao 2012-05-29
  • 打赏
  • 举报
回复
还请说下详细的步骤啊^以便于对递归不理解的同志们,进行学习啊!
W170532934 2012-05-28
  • 打赏
  • 举报
回复
既然楼主说不可能出现那种乱的顺序,那就用递归去查看每一个字符。如果等就继续查看下一个呗。
babaluoshahao 2012-05-28
  • 打赏
  • 举报
回复
这个代码,是我,抄写别人的,所以没有注释,大家见谅啊……
babaluoshahao 2012-05-28
  • 打赏
  • 举报
回复
答案是有的,不过不是很理解……我贴出来大家讨论下,谁能把其模型或原理分析下……
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

void PrintfArray(char *p_str, char *s_str, int *print_arr, int p_len, int s_len, int print_arr_num, int p_start_num, int s_start_num);
void ConnectSequence(char *p_str, char *s_str);

int main(void)
{
char *ParString = "abcbcc";
char *SonString = "abc";

ConnectSequence(ParString, SonString);

return 0;
}

void PrintfArray(char *p_str, char *s_str, int *print_arr, int p_len, int s_len, int print_arr_num, int p_start_num, int s_start_num)
{
int pStartnum = p_start_num;
int sStartnum = s_start_num;

int printNum = print_arr_num;
int i, j;

if (printNum == s_len)
{
for (i=0; i<s_len; i++)
{
cout<<*(print_arr+i);
cout<<" ";
}
cout << endl;
}

for (i=pStartnum; i<p_len; i++)
{
for (j=sStartnum; j<s_len; j++)
{
if (*(p_str+i) == *(s_str+j))
{
print_arr[printNum] = i+1;
pStartnum = i;
sStartnum = j;

PrintfArray(p_str, s_str, print_arr, p_len, s_len, printNum+1, pStartnum+1, sStartnum+1);

}
}
}
}

void ConnectSequence(char *p_str, char *s_str)
{
int p_len = strlen(p_str);
int s_len = strlen(s_str);

int *print_arr = new int[s_len];

unsigned int print_arr_num = 0;

if (NULL == p_str && NULL == s_str)
{
cout<<"string error"<<endl;
return;
}

if (NULL == print_arr)
{
cout<<"string error"<<endl;
return;
}

PrintfArray(p_str, s_str, print_arr, p_len, s_len, 0, 0, 0);
}
鬼竹影 2012-05-28
  • 打赏
  • 举报
回复
看懂题了,不懂解法,等待答案
yuexicheng 2012-05-28
  • 打赏
  • 举报
回复
呵呵,明白了,谢谢啊,不过用递归解我一点思路也没有,嘿嘿只好等等看别人怎么解了
babaluoshahao 2012-05-28
  • 打赏
  • 举报
回复
你问的问题,果然犀利,出题者的意图是,不必打印出143,只能是顺序,不会小的下标在前大的下标前面……
加载更多回复(4)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧