Skip to content

Commit 9078a6f

Browse files
committed
Oct 12
1 parent 6647146 commit 9078a6f

File tree

9 files changed

+459
-6
lines changed

9 files changed

+459
-6
lines changed
Binary file not shown.

2019/oct12/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2016-2017 ACM-ICPC East Central North America Regional Contest (ECNA 2016)
2+
<https://codeforces.com/group/L0zlhp0AHw/contest/101196>

2019/oct12/b-foosball.cpp

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <set>
4+
#include <queue>
5+
#include <utility>
6+
using namespace std;
7+
8+
void debug() {
9+
cout << "DEBUG" << endl;
10+
}
11+
12+
typedef pair<int, int> player_pair;
13+
14+
/*
15+
Didn't get this one
16+
17+
g++ -std=c++11 b-foosball.cpp -o k
18+
19+
6
20+
Balaji David Alex Scott Andrew Ravi
21+
WWBWBBWBW
22+
23+
6
24+
Amy Jinu Kasey Sarah Sheetal Julia
25+
BBBBB
26+
27+
10
28+
a b c d e f g h i j
29+
WW
30+
31+
10
32+
a b c d e f g h i j
33+
WWBB
34+
35+
10
36+
a b c d e f g h i j
37+
WBBBW
38+
39+
5
40+
a b c d e
41+
B
42+
o:
43+
b d
44+
45+
5
46+
a b c d e
47+
BW
48+
o:
49+
b d
50+
a e
51+
52+
5
53+
a b c d e
54+
BWW
55+
o:
56+
a e
57+
*/
58+
int main() {
59+
queue<int> q;
60+
int n, wo, wd, bo, bd, tmp;
61+
wo = 0; bo = 1; wd = 2; bd = 3;
62+
int wa1, wa2, ba1, ba2;
63+
wa1 = 0; wa2 = 2; ba1 = 1; ba2 = 3;
64+
player_pair pp;
65+
set<player_pair> spp;
66+
vector<player_pair> vpp;
67+
int k; k = 0; // used to find len of dynasty
68+
int acc; acc = 1; // acc to track len of longest dynasty
69+
string outcomes;
70+
71+
cin >> n;
72+
string names[n];
73+
for(int i = 0; i < n; i++) {
74+
cin >> names[i];
75+
if(i >= 4) q.push(i);
76+
}
77+
cin >> outcomes;
78+
79+
int i = 0;
80+
for(; i < outcomes.size(); i++) {
81+
if(i > 0 && outcomes[i] != outcomes[i - 1]) {
82+
// change dynasty
83+
//cout << k << " " << acc << endl;
84+
if(k >= acc) {
85+
if(outcomes[i - 1] == 'W') // w dynasty record
86+
pp = player_pair(wa1, wa2);
87+
else // b dynasty recod
88+
pp = player_pair(ba1, ba2);
89+
90+
if(k > acc) {
91+
spp = set<player_pair>();
92+
vpp = vector<player_pair>();
93+
}
94+
95+
if(!spp.count(pp)) {
96+
spp.insert(pp);
97+
vpp.push_back(pp);
98+
}
99+
acc = k;
100+
}
101+
k = 1;
102+
} else {
103+
// dynasty continued
104+
k++;
105+
}
106+
if(outcomes[i] == 'W') { // w wins
107+
swap(wo, wd);
108+
tmp = bd;
109+
bd = bo;
110+
q.push(tmp);
111+
bo = q.front();
112+
q.pop();
113+
ba1 = bd;
114+
ba2 = bo;
115+
} else { // b wins
116+
swap(bo, bd);
117+
tmp = wd;
118+
wd = wo;
119+
q.push(tmp);
120+
wo = q.front();
121+
q.pop();
122+
wa1 = wd;
123+
wa2 = wo;
124+
}
125+
}
126+
127+
if(k >= acc) {
128+
if(outcomes[i - 1] == 'W') // w dynasty record
129+
pp = player_pair(wa1, wa2);
130+
else // b dynasty recod
131+
pp = player_pair(ba1, ba2);
132+
133+
//cout << k << " " << acc << endl;
134+
if(k > acc) {
135+
spp = set<player_pair>();
136+
vpp = vector<player_pair>();
137+
}
138+
139+
if(!spp.count(pp)) {
140+
spp.insert(pp);
141+
vpp.push_back(pp);
142+
}
143+
}
144+
145+
for(const player_pair &ppo : vpp)
146+
cout << names[ppo.first] << " " << names[ppo.second] << endl;
147+
148+
return 0;
149+
}

2019/oct12/c-key.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <cctype>
4+
#include <cmath>
5+
#include <utility>
6+
using namespace std;
7+
8+
void debug() {
9+
cout << "DEBUG" << endl;
10+
}
11+
12+
char encode(char ch, int key) {
13+
ch = ((ch - 'A') + (key - 'A') + 26 ) % 26;
14+
ch += 'A';
15+
return ch;
16+
}
17+
18+
char decode(char ch, char key) {
19+
ch = ((ch - 'A') - (key - 'A') + 26 ) % 26;
20+
ch += 'A';
21+
return ch;
22+
}
23+
24+
// INV: in.size() <= key.size()
25+
string decode_substr(string in, string key) {
26+
string out;
27+
for(int i = 0; i < in.size(); i++) {
28+
out += decode(in[i], key[i]);
29+
}
30+
return out;
31+
}
32+
33+
/*
34+
https://stackoverflow.com/questions/522778/caesar-cipher-in-c
35+
g++ -std=c++11 c-key.cpp -o k
36+
37+
SGZVQBUQAFRWSLC ACM
38+
SGZVQBUQAFR ACM
39+
SGZVQBUQ ACM
40+
SGZ ACM
41+
*/
42+
int main() {
43+
string in, out, key;
44+
int k = 0;
45+
cin >> in >> key;
46+
int m = key.size();
47+
while(k < in.size()) {
48+
out += decode_substr(in.substr(k, min(m, (int) in.size() - k)), key);
49+
key = out.substr(k, m);
50+
k += m;
51+
}
52+
cout << out << endl;
53+
54+
return 0;
55+
}

2019/oct12/d-translation.cpp

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <map>
4+
#include <queue>
5+
#include <string>
6+
using namespace std;
7+
8+
struct Edge {
9+
int vertex;
10+
Edge *next;
11+
};
12+
13+
class Graph {
14+
public:
15+
Graph(int n) {
16+
if(n <= 0)
17+
throw std::invalid_argument( "n must be positive" );
18+
size = n;
19+
vertices = new Edge*[size];
20+
for(int i = 0; i < n; i++)
21+
vertices[i] = NULL;
22+
}
23+
24+
/* destructor */
25+
~Graph() {
26+
internal_destructor();
27+
}
28+
29+
/* copy constructor */
30+
Graph(const Graph &g) {
31+
internal_copy(g);
32+
}
33+
34+
/* assignment operator */
35+
Graph& operator=(const Graph &g) {
36+
if(this == &g) return *this;
37+
internal_destructor();
38+
internal_copy(g);
39+
return *this;
40+
}
41+
42+
int get_size() { return size; }
43+
44+
/* returns true iff edge has been added successfully */
45+
bool add_edge(int from, int to) {
46+
if(from >= size || to >= size || from < 0 || to < 0)
47+
return false;
48+
Edge *my_edge = new Edge{to, NULL};
49+
if(vertices[from]) {
50+
Edge *curr = vertices[from];
51+
while(curr->vertex != to && curr->next)
52+
curr = curr->next;
53+
if(curr->vertex == to)
54+
return false;
55+
curr->next = my_edge;
56+
} else {
57+
vertices[from] = my_edge;
58+
}
59+
return true;
60+
}
61+
62+
/* returns true iff edge has been removed successfully */
63+
bool rem_edge(int from, int to) {
64+
if(from >= size || to >= size || from < 0 || to < 0)
65+
return false;
66+
if(vertices[from]) {
67+
Edge *curr = vertices[from];
68+
if(curr->vertex == to) {
69+
vertices[from] = curr->next;
70+
delete curr;
71+
return true;
72+
} else {
73+
Edge *tmp;
74+
while(curr->next) {
75+
if(curr->next->vertex == to) {
76+
tmp = curr->next;
77+
curr->next = curr->next->next;
78+
delete tmp;
79+
return true;
80+
}
81+
curr = curr->next;
82+
}
83+
}
84+
}
85+
return false;
86+
}
87+
88+
Edge* neighbors(int vertex) {
89+
if(vertex < 0 || vertex >= size)
90+
return NULL;
91+
else
92+
return vertices[vertex];
93+
}
94+
95+
private:
96+
int size;
97+
Edge **vertices;
98+
99+
void internal_destructor() {
100+
Edge *temp; Edge *curr;
101+
for(int i = 0; i < size; i++) {
102+
curr = vertices[i];
103+
while(curr) {
104+
temp = curr; curr = curr->next;
105+
delete temp;
106+
}
107+
}
108+
delete[] vertices;
109+
}
110+
111+
void internal_copy(const Graph &g) {
112+
size = g.size;
113+
Edge *curr;
114+
Edge *g_curr;
115+
vertices = new Edge*[size];
116+
for(int i = 0; i < size; i++) {
117+
g_curr = g.vertices[i];
118+
if(g_curr) {
119+
curr = new Edge{g_curr->vertex, NULL};
120+
vertices[i] = curr;
121+
while(g_curr->next) {
122+
g_curr = g_curr->next;
123+
curr->next = new Edge{g_curr->vertex, NULL};
124+
curr = curr->next;
125+
}
126+
} else {
127+
vertices[i] = NULL;
128+
}
129+
}
130+
}
131+
};
132+
133+
bool breadth_first_search(Graph &g, int start, int target) {
134+
bool visited[g.get_size()];
135+
for(int i = g.get_size() - 1; i >= 0; i--)
136+
visited[i] = false;
137+
queue<int> q; q.push(start); // current level
138+
queue<int> l; // next level
139+
Edge *nbr;
140+
int curr;
141+
142+
while(!q.empty() || !l.empty()) {
143+
// if q empty, then fetch the next level to visit
144+
if(q.empty())
145+
while(!l.empty()) {
146+
q.push(l.front()); l.pop();
147+
}
148+
149+
// get vertex
150+
curr = q.front(); q.pop();
151+
// check vertex is visited
152+
if(!visited[curr]) {
153+
// check vertex is target
154+
printf("%d ",curr);
155+
if(curr == target) {
156+
printf("\n");
157+
return true;
158+
}
159+
visited[curr] = true;
160+
// add neighbors to next level
161+
nbr = g.neighbors(curr);
162+
while(nbr) {
163+
l.push(nbr->vertex);
164+
nbr = nbr->next;
165+
}
166+
}
167+
}
168+
169+
printf("\n");
170+
return false;
171+
}
172+
173+
int main() {
174+
175+
Graph g = Graph(4);
176+
g.add_edge(0, 1);
177+
g.add_edge(1, 2);
178+
g.add_edge(2, 3);
179+
cout << breadth_first_search(g, 0, 3) << endl;
180+
g.rem_edge(1, 2);
181+
cout << breadth_first_search(g, 0, 3) << endl;
182+
g.add_edge(1, 3);
183+
cout << breadth_first_search(g, 0, 3) << endl;
184+
185+
return 0;
186+
}

0 commit comments

Comments
 (0)