Print all possible strings of length k that can be formed from a set of n characters
2015-01-26 19:16
399 查看
Given a set of characters and a positive integer k, print all possible strings of length k that can be formed from the given set.
Examples:
Input: set[] = {'a', 'b'}, k = 3Output:aaaaababaabbbaababbbabbbInput: set[] = {'a', 'b', 'c', 'd'}, k = 1Output:abcd
Given a set of characters and a positive integer k, print all possible strings of length k that can be formed from the given set.
Examples:
Input: set[] = {'a', 'b'}, k = 3Output:aaaaababaabbbaababbbabbbInput: set[] = {'a', 'b', 'c', 'd'}, k = 1Output:abcd
//
// main.cpp
// stringOutput
//
// Created by Libin Liang on 1/26/15.
// Copyright (c) 2015 com.libin. All rights reserved.
//
#include <iostream>
#include <string>
#include <vector>
using namespace
std;
void printVtr(vector<char> &vtr)
{
for (int i=0; i<vtr.size(); i++)
printf ("%c ", vtr[i]);
printf("\n");
return;
}
void foo(string str,
int k, vector<char> &vtr)
{
if (k == 0)
{
printVtr(vtr);
return;
}
for (int i=0; i<str.size(); i++)
{
vtr.push_back(str.at(i));
foo(str, k-1, vtr);
vtr.pop_back();
}
}
void outputString(string str,
int k)
{
if (str.size() ==
0 || k <= 0)
return;
vector<char> vtr;
for (int i=0; i<str.size(); i++)
{
vtr.push_back(str.at(i));
foo(str, k-1, vtr);
vtr.pop_back();
}
return;
}
int main(int argc,
const char * argv[]) {
outputString("ab",
3);
return 0;
}
Input: set[] = {'a', 'b'}, k = 3Output:aaaaababaabbbaababbbabbbInput: set[] = {'a', 'b', 'c', 'd'},
k = 1Output:abcd
Examples:
Input: set[] = {'a', 'b'}, k = 3Output:aaaaababaabbbaababbbabbbInput: set[] = {'a', 'b', 'c', 'd'}, k = 1Output:abcd
Given a set of characters and a positive integer k, print all possible strings of length k that can be formed from the given set.
Examples:
Input: set[] = {'a', 'b'}, k = 3 Output: aaa aab aba abb baa bab bba bbb Input: set[] = {'a', 'b', 'c', 'd'}, k = 1 Output: a b c d
Input: set[] = {'a', 'b'}, k = 3Output:aaaaababaabbbaababbbabbbInput: set[] = {'a', 'b', 'c', 'd'}, k = 1Output:abcd
//
// main.cpp
// stringOutput
//
// Created by Libin Liang on 1/26/15.
// Copyright (c) 2015 com.libin. All rights reserved.
//
#include <iostream>
#include <string>
#include <vector>
using namespace
std;
void printVtr(vector<char> &vtr)
{
for (int i=0; i<vtr.size(); i++)
printf ("%c ", vtr[i]);
printf("\n");
return;
}
void foo(string str,
int k, vector<char> &vtr)
{
if (k == 0)
{
printVtr(vtr);
return;
}
for (int i=0; i<str.size(); i++)
{
vtr.push_back(str.at(i));
foo(str, k-1, vtr);
vtr.pop_back();
}
}
void outputString(string str,
int k)
{
if (str.size() ==
0 || k <= 0)
return;
vector<char> vtr;
for (int i=0; i<str.size(); i++)
{
vtr.push_back(str.at(i));
foo(str, k-1, vtr);
vtr.pop_back();
}
return;
}
int main(int argc,
const char * argv[]) {
outputString("ab",
3);
return 0;
}
Input: set[] = {'a', 'b'}, k = 3Output:aaaaababaabbbaababbbabbbInput: set[] = {'a', 'b', 'c', 'd'},
k = 1Output:abcd
相关文章推荐
- CareerCup Output the amount of all possible strings of length N that don't of have consecutive a,b,c
- Given a set of n symbols a size k and a combination of length k of non repeating characters from the symbol set
- 给定一个set字符和一个正数k,找出所有该做set它可以由长度构成k该字符串集合 print-all-combinations-of-given-length
- Tip Of The Day : Be All That You Can Be
- 给出一个set的字符和一个正数k,求所有由这个set能组成长度为k的字符串集合 print-all-combinations-of-given-length
- eclise 部署web工程报 There are no resources that can be added or removed from the server.
- startIndex can not be larger than length of string. Parameter name: startIndex
- JDBC操作MySQL出现:This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, ...的问题解决
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- What can I learn right now in just 10 minutes that could be useful for the rest of my life?
- 爬网日志中的警告信息:文件达到最大下载次数,The file reached the maximum download limit. Check that the full text of the document can be meani
- Unity中BUG:SetDestination can only be called on an active agent that has been placed on a navmesh 的处理
- 导出问题,Length of cell contents (text) 32,767 characters. Only 1,024 display in a cell; all 32,767 di
- dataframe插入数据报错SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a
- There are no resources that can be added or removed from the server(转)
- Eclipse 报错There are no resources that can be added or removed from the server
- There are no resources that can be added or removed from the server.
- What can I learn right now in just 10 minutes that could be useful for the rest of my life?
- 【BCB】ocidl.h E2337 Only one of a set of overloaded functions can be ”C“ 报错解决办法
- Given an array of size n, find all the possible sub set of the array of size k