[笔试面试][code_by_hand]输出第二个字符串在第一个字符串中的连接次序


1
#include <iostream> 2 #include <string> 3 #include <vector> 4 using namespace std; 5 6 //摘自程序员面试宝典 7 //输入两个字符串,比如abdbcc和abc,输出第二个字符串在第一个字符串中的连接次序 8 //即输出125,126,145,146 9 template <class T> 10 void print(vector<T> v) 11 { 12 for (typename vector<T>::iterator it = v.begin(); it != v.end(); it++) 13 { 14 cout<<*it<<" "; 15 } 16 cout<<endl; 17 } 18 19 void print(char str1[], char str2[], int index1, int index2, vector<int> &v) 20 { 21 if (str1==NULL || str2==NULL) 22 { 23 return; 24 } 25 if (str1[index1]=='\0' || str2[index2]=='\0') 26 { 27 if (!(str1[index1]=='\0' && str2[index2]!='\0') ) 28 { 29 print(v); 30 } 31 return; 32 } 33 if (str1[index1]==str2[index2]) 34 { 35 v.push_back(index1); 36 print(str1, str2, index1+1, index2+1, v); 37 v.pop_back(); 38 } 39 print(str1, str2, index1+1, index2, v); 40 } 41 42 int main() 43 { 44 char * str1 = "abdbcc"; 45 char * str2 = "abcd"; 46 int index1 = 0, index2 = 0; 47 vector<int> v; 48 print(str1, str2, index1, index2, v); 49 return 0; 50 }

 

转载于:https://www.cnblogs.com/wendelhuang/archive/2013/05/09/3068487.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值