输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序(递归方法)
2014-09-29 16:34
344 查看
[cpp] view
plaincopy
//************************************************************************
//功能:输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序。即输出125、126、145、146.
//参数:fir_str,第一个字符串;
// sec_str,第二个字符串;
// sec_str_length,第二个字符串长度;
// tab[],存放连接次序的数组;
// depth,字符串遍历深度;
// position,存放字符串2中每个字符在字符串1中的位置
//日期:2013.7.26
//*************************************************************************//
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void display(char *fir_str,char *sec_str,char sec_str_length,int tab[],char depth,int position)
{
int i;
if(depth==sec_str_length)
{
for(i=0;i<depth;i++)
{
printf("%d",tab[i]);
}
printf("\n");
}
else
{
while(*fir_str) //将字符串1中的字符同字符串2中的比较,直到找到与2中一样的字符sec_str才执行++
{
if(*fir_str== *sec_str)
{
tab[depth]=position+1;
display(fir_str+1,sec_str+1,sec_str_length,tab,depth+1,position+1);//这个地方的len在主函数调用时,用strlen(fir_str),因为fir_str变化了,所以len随之变化;tab不要写成了tab[]
}
fir_str++;
position++;
}
}
}
int main()
{
int tab[100];
char *string1="abdbcc";
char *string2="abc";
display(string1,string2,strlen(string2),tab,0,0);
system("pause");
return 0;
}
思路分析:
[cpp] view
plaincopy
/**************************************************************
举例:
char *string1="aabdbcc";
char *string2="abc";
'a'位置: 1 2
/ \ / \
'b'位置: 3 5 3 5
/ \ / \ / \ / \
'c'位置: 6 7 6 7 6 7 6 7
while大循环里,首先在string1中找到第一个‘a’字符,然后用递归方法在剩下的“abdbcc”中以同样的方式找‘b’,然后‘c’......
同理,第一次while循环结束,fir_str++,又在第二个位置发现了第二个‘a’,然后接着再递归找‘b’、‘c’,如此直到string1遍历完
while(*fir_str) //将字符串1中的字符同字符串2中的比较,直到找到与2中一样的字符sec_str才执行++
{
......;
fir_str++;
position++;
}
也可以这样写:
for(;*fir_str!='\0';fir_str++,position);
或许好理解一些。
plaincopy
//************************************************************************
//功能:输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序。即输出125、126、145、146.
//参数:fir_str,第一个字符串;
// sec_str,第二个字符串;
// sec_str_length,第二个字符串长度;
// tab[],存放连接次序的数组;
// depth,字符串遍历深度;
// position,存放字符串2中每个字符在字符串1中的位置
//日期:2013.7.26
//*************************************************************************//
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void display(char *fir_str,char *sec_str,char sec_str_length,int tab[],char depth,int position)
{
int i;
if(depth==sec_str_length)
{
for(i=0;i<depth;i++)
{
printf("%d",tab[i]);
}
printf("\n");
}
else
{
while(*fir_str) //将字符串1中的字符同字符串2中的比较,直到找到与2中一样的字符sec_str才执行++
{
if(*fir_str== *sec_str)
{
tab[depth]=position+1;
display(fir_str+1,sec_str+1,sec_str_length,tab,depth+1,position+1);//这个地方的len在主函数调用时,用strlen(fir_str),因为fir_str变化了,所以len随之变化;tab不要写成了tab[]
}
fir_str++;
position++;
}
}
}
int main()
{
int tab[100];
char *string1="abdbcc";
char *string2="abc";
display(string1,string2,strlen(string2),tab,0,0);
system("pause");
return 0;
}
思路分析:
[cpp] view
plaincopy
/**************************************************************
举例:
char *string1="aabdbcc";
char *string2="abc";
'a'位置: 1 2
/ \ / \
'b'位置: 3 5 3 5
/ \ / \ / \ / \
'c'位置: 6 7 6 7 6 7 6 7
while大循环里,首先在string1中找到第一个‘a’字符,然后用递归方法在剩下的“abdbcc”中以同样的方式找‘b’,然后‘c’......
同理,第一次while循环结束,fir_str++,又在第二个位置发现了第二个‘a’,然后接着再递归找‘b’、‘c’,如此直到string1遍历完
while(*fir_str) //将字符串1中的字符同字符串2中的比较,直到找到与2中一样的字符sec_str才执行++
{
......;
fir_str++;
position++;
}
也可以这样写:
for(;*fir_str!='\0';fir_str++,position);
或许好理解一些。
相关文章推荐
- 输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序(递归方法)
- 输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序(递归方法)
- 输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序
- 《程序员面试宝典》:输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序,即输出125,126,145,146
- 程序员面试宝典之输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序,即输出125,126,145,146
- 程序员面试宝典 8.2 典型递归问题: 面试例题1: 输入两个字符串,比如 abdbcc 和 abc, 输出第二个字符串在第一个字符串中的连接次序。即输出 125, 126, 145, 146.
- 输入两个字符串,比如abdcc和abc,输出第二个字符串在第一个字符串中的连接次序
- 输入两个字符串,比如abdbcc和abc,把abc在abdbcc中的连接次序输出
- 输入两个字符串,输出第二个字符串在第一个字符串中的连接次序
- 输出第二个字符串在第一个字符串中的连接次序
- 字符串问题:输出第二个字符串在第一个字符串中的连接次序
- C语言:从键盘输入两个字符串,输出第一个字符串在第二个字符串中的位置
- java--封装一类Java对象,用户从输入对话框输入两个日期,程序将判断两个日期的大小关系(比如输出:您输入的第二个日期大于第一个日期)以及两个日期之间的间隔天数(比如输出:2006年6月6日和
- 输出第二个字符串在第一个字符串中的链接次序
- ACM-输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”
- 华为机试题:输入两个超长整型构成的字符串,其间使用一个空格分隔,每个字符串最大长度为100个字符。求第一个整数除以第二个整数以后的余数。。
- 输入两个字符串,从第一个字符串中删除第二个字符串中的所有字符,例如输入“They are student”,则删除之后的第一个字符串变成了“Thy r stdnts”
- 编写一个函数 接受两个字符串参数 如果 第一个参数被第二个包含 则输出第一个参数的首字符(图)
- 【程序员面试宝典】递归之两个字符串连接次序问题(P83)
- 将两个字符串连接起来取代第一个字符串。用string方法