-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequences.cpp
More file actions
46 lines (43 loc) · 1.08 KB
/
sequences.cpp
File metadata and controls
46 lines (43 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <string>
using std::string;
#include <vector>
using std::vector;
/* exercise: write a (probably recursive) function that returns
* all sequences of length n using characters from a given
* string. E.g., if s="abc" and n=2, then the output would
* be:
* aa
* ab
* ac
* ba
* bb
* bc
* ca
* cb
* cc
* */
vector<string> seq(string s, int n)
{
/* base case? */
if (n == 0) {
vector<string> R;
R.push_back("");
return R;
}
if (s == "") return vector<string>(); /* empty vector */
/* now the general case. if we had a magic box that
* computed this function for smaller inputs, how could
* we make use of it? Let's do induction on n. */
vector<string> T = seq(s,n-1); /* all n-1 length sequences */
/* now go through each element of T, and add each character
* in s... */
vector<string> R; /* for the return value */
for (size_t i = 0; i < T.size(); i++) {
for (size_t j = 0; j < s.length(); j++) {
R.push_back(T[i]+s[j]);
/* NOTE: T[i]+s[j] appends CHARACTER s[j] to STRING T[i] */
}
}
return R;
}
/* TODO: try to re-write this, and also test it out. */