Skip to content

Commit 021021b

Browse files
authored
GH-169: Solve codeforces 1796A, 1799A, 1800A (#192)
1 parent 1c0dfc0 commit 021021b

File tree

28 files changed

+665
-4
lines changed

28 files changed

+665
-4
lines changed

collections/codeforces/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17962,7 +17962,7 @@
1796217962
</tr>
1796317963
<tr>
1796417964
<tr>
17965-
<td>1796A</td>
17965+
<td>🟢&nbsp<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/codeforces1796A'>1796A</a></td>
1796617966
<td>1796B</td>
1796717967
<td>1796C</td>
1796817968
<td>1796D</td>
@@ -17992,7 +17992,7 @@
1799217992
</tr>
1799317993
<tr>
1799417994
<tr>
17995-
<td>1799A</td>
17995+
<td>🟢&nbsp<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/codeforces1799A'>1799A</a></td>
1799617996
<td>1799B</td>
1799717997
<td>1799C</td>
1799817998
<td>1799D</td>
@@ -18002,7 +18002,7 @@
1800218002
</tr>
1800318003
<tr>
1800418004
<tr>
18005-
<td>1800A</td>
18005+
<td>🟢&nbsp<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/codeforces1800A'>1800A</a></td>
1800618006
<td>1800B</td>
1800718007
<td>1800C</td>
1800818008
<td>1800D</td>

collections/codeforces/data.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,13 @@ problems:
7070
score: 1400
7171
- name: 492B
7272
languages: cpp
73-
score: 1200
73+
score: 1200
74+
- name: 1796A
75+
languages: cpp
76+
score: 800
77+
- name: 1799A
78+
languages: cpp
79+
score: 800
80+
- name: 1800A
81+
languages: cpp
82+
score: 800

problems/codeforces1796A/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
solution
2+
*.dSYM

problems/codeforces1796A/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Codeforces Problem
2+
3+
## Usage
4+
5+
Run program with an example
6+
7+
```
8+
bazel run cpp/main:solution < data/1.in
9+
```
10+
11+
Test program
12+
13+
```
14+
# Run all tests
15+
bazel test --test_output=all tests:solution_test
16+
bazel test --test_output=all --cache_test_results=no tests:solution_test
17+
# Run test with pecific test id
18+
bazel test --test_output=all tests:solution_test --test_arg=1
19+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
3+
cc_binary(
4+
name = "solution",
5+
srcs = ["solution.cpp"],
6+
visibility = ["//visibility:public"],
7+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <iostream>
4+
#include <map>
5+
#include <set>
6+
#include <string>
7+
#include <unordered_map>
8+
#include <vector>
9+
// #include <fstream>
10+
11+
using namespace std;
12+
13+
void solve(string fb){
14+
int n;
15+
string s;
16+
cin >> n;
17+
cin >> s;
18+
cout << (fb.find(s) == string::npos ? "NO" : "YES") << endl;
19+
}
20+
21+
int main() {
22+
ios::sync_with_stdio(false);
23+
cin.tie(0);
24+
25+
string fb = "";
26+
int i = 1;
27+
while(fb.size() < 100){
28+
if(i % 3 == 0){
29+
fb += "F";
30+
}
31+
if(i % 5 == 0){
32+
fb += "B";
33+
}
34+
i++;
35+
}
36+
37+
// filesystem::path filepath =
38+
// filesystem::current_path().parent_path().parent_path() / "data" /
39+
// "1.in";
40+
// ifstream file(filepath);
41+
// cin.rdbuf(file.rdbuf()); // redirect cin to file
42+
43+
int tt;
44+
cin >> tt;
45+
while (tt--) {
46+
// cout << tt << '\n';
47+
solve(fb);
48+
}
49+
return 0;
50+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
2+
3+
cc_test(
4+
name = "solution_test",
5+
srcs = ["solution_test.cpp"],
6+
deps = [
7+
"//problems/codeforces2A/src/main:solution",
8+
"@com_google_googletest//:gtest_main",
9+
],
10+
data = ["data"]
11+
)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <cstdlib>
2+
#include <filesystem>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <string>
6+
#include <string_view>
7+
#include <vector>
8+
9+
#include "gtest/gtest.h"
10+
11+
namespace fs = std::filesystem;
12+
13+
using namespace std;
14+
15+
std::string TEST_ID = "";
16+
17+
vector<string> split(string s, string delimiter) {
18+
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
19+
string token;
20+
vector<string> res;
21+
22+
while ((pos_end = s.find(delimiter, pos_start)) != string::npos) {
23+
token = s.substr(pos_start, pos_end - pos_start);
24+
pos_start = pos_end + delim_len;
25+
res.push_back(token);
26+
}
27+
28+
res.push_back(s.substr(pos_start));
29+
return res;
30+
}
31+
32+
std::string GetHello(std::string_view in) {
33+
if (in.size() == 0) {
34+
return std::string("hello, word");
35+
} else {
36+
return std::string("Hello, ") + in.data();
37+
}
38+
}
39+
40+
string ReadFile(const std::string &filename) {
41+
std::ifstream f(filename);
42+
string s((std::istreambuf_iterator<char>(f)),
43+
std::istreambuf_iterator<char>());
44+
return s;
45+
}
46+
47+
vector<string> RunProgram(string fileid) {
48+
vector<string> v;
49+
string program = "problems/codeforces2A/src/main/solution";
50+
string test_folder = "problems/codeforces2A/tests/data/";
51+
string command = program + " < " + test_folder + fileid + ".in > output.txt";
52+
std::system(command.c_str());
53+
string actual = ReadFile("output.txt");
54+
string expectedOutputFile = test_folder + fileid + ".out";
55+
string expected = ReadFile(expectedOutputFile);
56+
v.push_back(actual);
57+
v.push_back(expected);
58+
return v;
59+
}
60+
61+
TEST(RunTest, AllTestCases) {
62+
if (TEST_ID != "") {
63+
std::cout << "TEST_ID = " << TEST_ID << std::endl;
64+
} else {
65+
std::cout << "RUN ALL TESTS" << std::endl;
66+
}
67+
68+
std::string test_data_folder = "problems/codeforces2A/tests/data";
69+
for (const auto &entry : fs::directory_iterator(test_data_folder)) {
70+
string filename = split(entry.path(), "/").back();
71+
vector<string> v = split(filename, ".");
72+
string fileid = v[0];
73+
string extension = v[1];
74+
if (extension != "in"){
75+
continue;
76+
}
77+
if (TEST_ID != ""){
78+
if (fileid != TEST_ID){
79+
continue;
80+
}
81+
}
82+
// Run Test Case
83+
vector<string> output = RunProgram(fileid);
84+
string actual = output[0];
85+
string expected = output[1];
86+
string message = "❌ FAIL CASE: " + fileid;
87+
ASSERT_EQ(actual, expected) << message;
88+
}
89+
}
90+
91+
int main(int argc, char **argv) {
92+
::testing::InitGoogleTest(&argc, argv);
93+
if (argc == 2) {
94+
TEST_ID = argv[1];
95+
}
96+
return RUN_ALL_TESTS();
97+
}

problems/codeforces1796A/data/1.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
3
2+
3
3+
FFB
4+
8
5+
BFFBFFBF
6+
3
7+
BBB

problems/codeforces1796A/data/1.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
YES
2+
YES
3+
NO

problems/codeforces1799A/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
solution
2+
*.dSYM

problems/codeforces1799A/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Codeforces Problem
2+
3+
## Usage
4+
5+
Run program with an example
6+
7+
```
8+
bazel run cpp/main:solution < data/1.in
9+
```
10+
11+
Test program
12+
13+
```
14+
# Run all tests
15+
bazel test --test_output=all tests:solution_test
16+
bazel test --test_output=all --cache_test_results=no tests:solution_test
17+
# Run test with pecific test id
18+
bazel test --test_output=all tests:solution_test --test_arg=1
19+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
3+
cc_binary(
4+
name = "solution",
5+
srcs = ["solution.cpp"],
6+
visibility = ["//visibility:public"],
7+
)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
// #include <fstream>
4+
#include <iostream>
5+
#include <map>
6+
#include <set>
7+
#include <string>
8+
#include <unordered_map>
9+
#include <vector>
10+
11+
using namespace std;
12+
13+
void solve() {
14+
int n;
15+
int m;
16+
cin >> n;
17+
cin >> m;
18+
vector<int> p(m);
19+
for (int i = 0; i < m; i++) {
20+
cin >> p[i];
21+
}
22+
vector<int> output(n, -1);
23+
set<int> actions;
24+
int last_posts = n-1;
25+
for (int i = 0; i < m; i++) {
26+
if(actions.find(p[i]) == actions.end()) {
27+
actions.insert(p[i]);
28+
if(last_posts >= 0){
29+
output[last_posts] = i + 1;
30+
last_posts--;
31+
}
32+
}
33+
}
34+
35+
for (auto item : output) {
36+
cout << item << " ";
37+
}
38+
cout << endl;
39+
}
40+
41+
int main() {
42+
ios::sync_with_stdio(false);
43+
cin.tie(0);
44+
45+
// filesystem::path filepath =
46+
// filesystem::current_path().parent_path().parent_path() / "data" / "1.in";
47+
// ifstream file(filepath);
48+
// cin.rdbuf(file.rdbuf()); // redirect cin to file
49+
50+
int tt;
51+
cin >> tt;
52+
int n, m;
53+
while (tt--) {
54+
solve();
55+
}
56+
return 0;
57+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
2+
3+
cc_test(
4+
name = "solution_test",
5+
srcs = ["solution_test.cpp"],
6+
deps = [
7+
"//problems/codeforces2A/src/main:solution",
8+
"@com_google_googletest//:gtest_main",
9+
],
10+
data = ["data"]
11+
)

0 commit comments

Comments
 (0)