forked from projectX-india/madlab-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA2_Weak_Typing_Chapter_2.cpp
190 lines (128 loc) · 4.79 KB
/
A2_Weak_Typing_Chapter_2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <bits/stdc++.h>
#include <iostream>
#include <string.h>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
long printAllSubstrings(string input_string, long n)
{
/*
* Fix start index in outer loop.
* Reveal new character in inner loop till end of string.
* Print till-now-formed string.
*/
// PRINTING SUBSTRINGS
long min_count = 0;
int hand = 0;
for (long i = 0; i < n; i++)
{
char temp[n - i + 1];
//cout << "I is :" << i << "\n";
long tempindex = 0;
for (long j = i; j < n; j++)
{
temp[tempindex++] = input_string[j];
if ( tempindex == 0 ){
if ( input_string[j] == 'X'){
hand = 1;
//flag = 0;
// cout << input_string[k];
}else if ( input_string [j] == 'O'){
hand = 0;
//flag = 0;
// cout << input_string[k];
}
}
if ( tempindex != 0 ){
if ( input_string[j] == 'X' && hand == 0 ){
min_count = min_count + 1;
// cout << i << " X here";
hand = 1;
}else if ( input_string[j] == 'O' && hand == 1){
min_count = min_count + 1;
// cout << input_string[k];
// cout << "O here" << i;
hand =0;
}
}
temp[tempindex] = '\0';
//printf("%s\n", temp);
}
}
return min_count;
}
int main(){
ofstream file_print;
file_print.open("output_A2_Protect.txt");
ifstream inFile;
string temp;
inFile.open("Input_A2_Protect.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
long testcases;
getline( inFile, temp) ;
stringstream geek(temp);
geek >> testcases;
//testcases = int(temp);
for( long testcase_loop = 0 ; testcase_loop < testcases ; testcase_loop++){
long char_length;
getline ( inFile, temp );
stringstream geek ( temp );
geek >> char_length;
//cin >> char_length;
long min_count = 0;
int flag = 1;
int hand = 0;
// 0 for O, 1 for X
string input_string;
getline (inFile, input_string);
min_count = printAllSubstrings( input_string , char_length);
/*
for (long len = 1; len <= char_length; len++) {
// Pick ending point
for (long i = 0; i <= char_length - len; i++) {
// Print characters from current
// starting point to current ending
// point.
long j = i + len - 1;
for (long k = i; k <= j; k++){
cout << input_string[k];
if ( flag == 1 ){
if ( input_string[k] == 'X'){
hand = 1;
flag = 0;
// cout << input_string[k];
}else if ( input_string [k] == 'O'){
hand = 0;
flag = 0;
// cout << input_string[k];
}
}
if ( flag == 0 ){
if ( input_string[k] == 'X' && hand == 0 ){
min_count = min_count + 1;
// cout << i << " X here";
hand = 1;
}else if ( input_string[k] == 'O' && hand == 1){
min_count = min_count + 1;
// cout << input_string[k];
// cout << "O here" << i;
hand =0;
}
}
}
cout << "\n";
cout << "Min Count:" << min_count << "\n";
flag = 1;
}
*/
cout << min_count % 1000000007 << "\n";
file_print << "Case #"<<testcase_loop+1<<": "<< min_count % 1000000007 << "\n";
}
file_print.close();
inFile.close();
return 0;
}