-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest1.cpp
More file actions
67 lines (60 loc) · 1.43 KB
/
Copy pathtest1.cpp
File metadata and controls
67 lines (60 loc) · 1.43 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
using namespace std;
void printLeast(string arr)
{
// min_avail represents the minimum number which is
// still available for inserting in the output vector.
// pos_of_I keeps track of the most recent index
// where 'I' was encountered w.r.t the output vector
int min_avail = 1, pos_of_I = 0;
//vector to store the output
vector<int>v;
// cover the base cases
if (arr[0]=='I')
{
v.push_back(1);
v.push_back(2);
min_avail = 3;
pos_of_I = 1;
}
else
{
v.push_back(2);
v.push_back(1);
min_avail = 3;
pos_of_I = 0;
}
// Traverse rest of the input
for (int i=1; i<arr.length(); i++)
{
if (arr[i]=='I')
{
v.push_back(min_avail);
min_avail++;
pos_of_I = i+1;
}
else
{
v.push_back(v[i]);
for (int j=pos_of_I; j<=i; j++)
v[j]++;
min_avail++;
}
}
// print the number
for (int i=0; i<v.size(); i++)
cout << v[i] << " ";
cout << endl;
}
// Driver program to check the above function
int main()
{
printLeast("IDID");
printLeast("I");
printLeast("DD");
printLeast("II");
printLeast("DIDI");
printLeast("IIDDD");
printLeast("DDIDDIID");
return 0;
}