Skip to content

Commit d2d60ff

Browse files
committed
Weekly Contest 86
1 parent 423298c commit d2d60ff

8 files changed

+228
-0
lines changed

find-and-replace-in-string.cc

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Find And Replace in String
2+
#define ALL(x) (x).begin(), (x).end()
3+
4+
class Solution {
5+
public:
6+
string findReplaceString(string S, vector<int>& indexes, vector<string>& sources, vector<string>& targets) {
7+
int m = indexes.size();
8+
vector<int> p(m);
9+
iota(ALL(p), 0);
10+
sort(ALL(p), [&](int a, int b) {return indexes[a] < indexes[b];});
11+
string r;
12+
int n = S.size();
13+
for (int j = 0, i = 0; i < n; ) {
14+
while (j < indexes.size() && indexes[p[j]] < i) j++;
15+
while (j < indexes.size() && indexes[p[j]] == i) {
16+
if (i + sources[p[j]].size() <= n &&
17+
S.compare(i, sources[p[j]].size(), sources[p[j]]) == 0) {
18+
i += sources[p[j]].size();
19+
r += targets[p[j]];
20+
goto next;
21+
}
22+
j++;
23+
}
24+
r += S[i++];
25+
next:;
26+
}
27+
return r;
28+
}
29+
};

flipping-an-image.cc

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Flipping an Image
2+
#define ALL(x) (x).begin(), (x).end()
3+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
4+
#define REP(i, n) FOR(i, 0, n)
5+
6+
class Solution {
7+
public:
8+
vector<vector<int>> flipAndInvertImage(vector<vector<int>> &A) {
9+
int m = A.size(), n = A[0].size();
10+
REP(i, m) {
11+
reverse(ALL(A[i]));
12+
REP(j, n)
13+
A[i][j] ^= 1;
14+
}
15+
return A;
16+
}
17+
};

guess-the-word.cc

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Guess the Word
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
int same(string& a, string& b) {
7+
int c = 0;
8+
REP(i, 6)
9+
c += a[i] == b[i];
10+
return c;
11+
}
12+
public:
13+
void findSecretWord(vector<string>& ws, Master& master) {
14+
while (ws.size()) {
15+
int opt = ws.size()+1, opt_i = -1;
16+
REP(i, ws.size()) {
17+
int cnt[7] = {};
18+
REP(j, ws.size())
19+
cnt[same(ws[i], ws[j])]++;
20+
int t = *max_element(cnt, cnt+7);
21+
if (t < opt)
22+
opt = t, opt_i = i;
23+
}
24+
string g = ws[opt_i];
25+
ws.erase(ws.begin()+opt_i);
26+
int t = master.guess(g);
27+
ws.erase(remove_if(ws.begin(), ws.end(),
28+
[&](string& s) { return same(g, s) != t; }),
29+
ws.end());
30+
}
31+
}
32+
};

image-overlap.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Image Overlap
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
8+
int n = A.size(), r = 0;
9+
FOR(dx, -n+1, n)
10+
FOR(dy, -n+1, n) {
11+
int o = 0;
12+
REP(i, n)
13+
REP(j, n) {
14+
int ii = i+dx, jj = j+dy;
15+
if (B[i][j] && unsigned(ii) < n && unsigned(jj) < n && A[ii][jj])
16+
o++;
17+
}
18+
r = max(r, o);
19+
}
20+
return r;
21+
}
22+
};

keys-and-rooms.cc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Keys and Rooms
2+
class Solution {
3+
public:
4+
bool canVisitAllRooms(vector<vector<int>>& rs) {
5+
int n = rs.size();
6+
vector<int> st{0};
7+
vector<char> c(n);
8+
c[0] = 1;
9+
while (st.size()) {
10+
n--;
11+
int x = st.back();
12+
st.pop_back();
13+
for (int y: rs[x])
14+
if (!c[y]) {
15+
c[y] = 1;
16+
st.push_back(y);
17+
}
18+
}
19+
return !n;
20+
}
21+
};

magic-squares-in-grid.cc

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Magic Squares In Grid
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
int numMagicSquaresInside(vector<vector<int>>& a) {
8+
int n = a.size(), s = 0;
9+
REP(i, n-2)
10+
REP(j, n-2) {
11+
char b[10] = {};
12+
REP(ii, 3)
13+
REP(jj, 3) {
14+
if (a[i+ii][j+jj] > 9) goto nxt;
15+
b[a[i+ii][j+jj]] = 1;
16+
}
17+
FOR(i, 1, 10) if (!b[i]) goto nxt;
18+
REP(k, 3) {
19+
if (a[i+k][j]+a[i+k][j+1]+a[i+k][j+2] != 15) goto nxt;
20+
if (a[i][j+k]+a[i+1][j+k]+a[i+2][j+k] != 15) goto nxt;
21+
}
22+
if (a[i][j]+a[i+1][j+1]+a[i+2][j+2] != 15) goto nxt;
23+
s++;
24+
nxt:;
25+
}
26+
return s;
27+
}
28+
};
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Split Array into Fibonacci Sequence
2+
#define ALL(x) (x).begin(), (x).end()
3+
#define ROF(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (b); --i >= (a); )
4+
5+
class Solution {
6+
public:
7+
vector<int> splitIntoFibonacci(string S) {
8+
int n = S.size();
9+
long ta = 1, tb = 1;
10+
long oa = 0, ob, oc, a, b, c;
11+
vector<int> ret;
12+
ROF(i, 0, n) {
13+
oa += ta*(S[i]-'0');
14+
ta *= 10;
15+
if (oa > INT_MAX) break;
16+
ob = 0;
17+
tb = 1;
18+
ROF(j, 0, i) {
19+
ob += tb*(S[j]-'0');
20+
tb *= 10;
21+
if (ob > oa) break;
22+
int k = j-1;
23+
ret.clear();
24+
ret.push_back(a = oa);
25+
ret.push_back(b = ob);
26+
while (k >= 0) {
27+
c = a-b;
28+
oc = c;
29+
do if (S[k]-'0' != oc % 10) goto nxt;
30+
while (k--, oc /= 10);
31+
a = b;
32+
b = c;
33+
ret.push_back(c);
34+
}
35+
nxt:
36+
if (k < 0 && ret.size() >= 3) {
37+
reverse(ALL(ret));
38+
return ret;
39+
}
40+
}
41+
}
42+
return {};
43+
}
44+
};

sum-of-distances-in-tree.cc

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Sum of Distances in Tree
2+
class Solution {
3+
vector<vector<int>> es;
4+
vector<int> ss, ans;
5+
int n;
6+
int f(int u, int p, int d) {
7+
int ds = d;
8+
ss[u] = 1;
9+
for (int v: es[u])
10+
if (v != p) {
11+
ds += f(v, u, d+1);
12+
ss[u] += ss[v];
13+
}
14+
return ds;
15+
}
16+
void g(int u, int p, int tot) {
17+
ans[u] = tot;
18+
for (int v: es[u])
19+
if (v != p)
20+
g(v, u, tot-ss[v]*2+n);
21+
}
22+
public:
23+
vector<int> sumOfDistancesInTree(int N, vector<vector<int>>& edges) {
24+
n = N;
25+
es.resize(N);
26+
ss.resize(N);
27+
ans.resize(N);
28+
for (auto& e:edges) {
29+
es[e[0]].push_back(e[1]);
30+
es[e[1]].push_back(e[0]);
31+
}
32+
g(0, -1, f(0, -1, 0));
33+
return ans;
34+
}
35+
};

0 commit comments

Comments
 (0)