字符串组合递归求解
来源:互联网 发布:mac能玩dota2吗 编辑:程序博客网 时间:2024/05/16 15:47
一 问题描述:
输入字符串,如abcde,求它的3的组合
二:代码
/*This is a free Program, You can modify or redistribute it under the terms of GNU*Description:字符串组合递归求解*Language: C++*Development Environment: VC6.0*Author: Wangzhicheng*E-mail: 2363702560@qq.com*Date: 2012/12/14*/#include <iostream>#include <string>#include <cstdlib>using namespace std;/**Solution类封装了使用了递归的组合算法*str:指向当前用户输入的字符串*tmp:指向组合的字符串*selectM:选取几个数来组合*/class Solution {private:char *str;char *tmp;int selectM;/**递归实现组合*selectStart:指向str中当前被操作的字符*selectCur:指向tmp中当前被操作的字符*/void Combine(int selectStart,int selectCur) {static int cnt;if(selectCur>=selectM) { //一种新的组合产生cout<<"第"<<++cnt<<"种组合是:"<<tmp<<endl;}else {int i;for(i=selectStart;str[i];i++) {tmp[selectCur]=str[i];Combine(i+1,selectCur+1);}}}public:Solution(const char *string,int selectM) {int n=strlen(string);str=new char[n+1];if(!str) exit(1);strncpy(str,string,n+1);this->selectM=selectM;tmp=new char[selectM+1];if(!tmp) exit(1);tmp[selectM+1]='\0';}void Combine() {Combine(0,0);}};void main() {Solution s("abcdef",2);s.Combine();}
三 测试
- 字符串组合递归求解
- 组合求解--递归法
- 递归求解字符串长度
- 递归求解字符串长度
- 递归求解字符串长度
- 用非递归方法实现 求解字符串组合的问题 JAVA代码
- 《剑指Offer》面试题:字符串的组合(不用递归求解)
- PHP字符串组合递归
- 递归的应用-组合数求解
- java字符串所有组合,字符串全组合--递归
- 递归求解二——寻找指定和的组合
- 字符串全组合-递归法
- 字符串的全组合非递归实现
- 使用递归输出字符串的组合
- 字符串的组合问题 递归和非递归实现
- 递归求解
- 递归求解
- 递归求解
- 再谈shell之“>/dev/null 2>&1”
- 阿越-临江仙:见《十字-声名雀起》
- Sql Server触发器
- spring下配置dbcp,c3p0,proxool
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- 字符串组合递归求解
- 街头美女怒砸豪车!女人不可得罪!
- (JNDI)Tomcat,Jsp配置JNDI连接Oracle,搞了好久,现在把东西总结一下。好拿来用。
- RTEMS 应用程序基本架构
- C面向对象编程示例——线程调度
- 博客笔记 字符串是否包含及匹配/查找/转换/拷贝问题
- 图形图像基本处理之——二值化、截取部分区域、提取质心
- sql server游标
- 坑爹的 APNS 回调方法didReceiveRemoteNotification