-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
64 lines (53 loc) · 885 Bytes
/
main.cpp
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
#include <bits/stdc++.h>
using namespace std;
int
countingValleys(int steps, string path)
{
int level=0;
int valley=0;
int count_valley=0;
for (int i=0; i<steps; i++)
{
if(path[i] == 'U')
level+=1;
else if (path[i] == 'D')
level-=1;
if(level < 0)
{
if (valley == 0)
valley = 1;
}
else if (valley == 1)
{
count_valley+=1;
valley=0;
}
}
return count_valley;
}
int
main(int argc, char **argv)
{
if (argc==1)
return 0;
ifstream filein (argv[1]);
string line;
int steps;
string path;
if (filein.is_open())
{
getline ( filein, line );
steps = stoi(line);
getline ( filein, line );
path = line;
filein.close();
}
else
{
cout << "Unable to open file";
return 0;
}
int result = countingValleys(steps, path);
cout << result << "\n";
return 0;
}