Skip to content

Commit 98c50f7

Browse files
committed
Philadelphia
1 parent e4e9500 commit 98c50f7

5 files changed

+160
-33
lines changed

basic-calculator-ii.nim

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import strutils
2+
3+
proc calculate(s: cstring): cint {.exportc.} =
4+
var
5+
i = 0
6+
sign = 1
7+
sum = 0
8+
while s[i].isSpaceAscii: inc i
9+
if s[i] == '-':
10+
inc i
11+
sign = -1
12+
13+
while true:
14+
while s[i].isSpaceAscii: inc i
15+
var x = s[i].ord-'0'.ord
16+
while (inc i; s[i].isDigit): x = x*10+s[i].ord-'0'.ord
17+
18+
while (while (s[i].isSpaceAscii): inc i; let op = s[i]; op == '*' or op == '/'):
19+
inc i
20+
while s[i].isSpaceAscii: inc i
21+
var y = s[i].ord-'0'.ord
22+
while (inc i; s[i].isDigit): y = y*10+s[i].ord-'0'.ord
23+
x = if op == '*': x*y else: x div y
24+
sum += sign*x
25+
while s[i].isSpaceAscii: inc i
26+
if s[i] == 0.char: break
27+
sign = if s[i] == '-': -1 else: 1
28+
inc i
29+
30+
return cast[cint](sum)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Check if Word Can Be Placed In Crossword
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+
bool placeWordInCrossword(vector<vector<char>>& g, string word) {
8+
int n = g.size(), m = g[0].size();
9+
REP(_, 2) {
10+
REP(i, n)
11+
REP(j, m)
12+
if (g[i][j] != '#') {
13+
int jj = j+1;
14+
while (jj < m && g[i][jj] != '#') jj++;
15+
16+
if (jj-j == word.size()) {
17+
int k = 0;
18+
for (; k < word.size() && (g[i][j+k] == ' ' || word[k] == g[i][j+k]); k++);
19+
if (k == word.size())
20+
return true;
21+
k = 0;
22+
for (; k < word.size() && (g[i][j+k] == ' ' || word[word.size()-1-k] == g[i][j+k]); k++);
23+
if (k == word.size())
24+
return true;
25+
}
26+
j = jj-1;
27+
}
28+
vector<vector<char>> h(m, vector<char>(n));
29+
REP(i, n)
30+
REP(j, m)
31+
h[j][i] = g[i][j];
32+
g.swap(h);
33+
swap(n, m);
34+
}
35+
return false;
36+
}
37+
};

detect-squares.nim

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Detect Squares
2+
import tables
3+
4+
const N = 1001
5+
6+
type DetectSquares {.exportc.} = object
7+
ls: Table[int, seq[int]]
8+
rs: Table[int, seq[int]]
9+
c: seq[int]
10+
11+
proc detectSquaresCreate(): ptr DetectSquares {.exportc.} =
12+
result = create DetectSquares
13+
result[] = DetectSquares(ls: initTable[int, seq[int]](), rs: initTable[int, seq[int]](), c: newSeq[int](N*N))
14+
15+
proc detectSquaresAdd(obj: ptr DetectSquares, point: ptr UncheckedArray[cint], pointSize: cint) {.exportc.} =
16+
let (x, y) = (cast[int](point[0]), cast[int](point[1]))
17+
obj.ls.mgetOrPut(x+y, @[]).add(y)
18+
obj.rs.mgetOrPut(x-y, @[]).add(y)
19+
obj.c[x+y*N] += 1
20+
21+
proc detectSquaresCount(obj: ptr DetectSquares, point: ptr UncheckedArray[cint], pointSize: cint): cint {.exportc.} =
22+
let (x0, y0) = (cast[int](point[0]), cast[int](point[1]))
23+
var res = 0
24+
for y1 in obj.ls.getOrDefault(x0+y0, @[]):
25+
if y0 == y1: continue
26+
let x1 = x0+y0-y1
27+
res += obj.c[x0+y1*N] * obj.c[x1+y0*N]
28+
for y1 in obj.rs.getOrDefault(x0-y0, @[]):
29+
if y0 == y1: continue
30+
let x1 = x0-y0+y1
31+
res += obj.c[x0+y1*N] * obj.c[x1+y0*N]
32+
cast[cint](res)
33+
34+
proc detectSquaresFree(obj: sink DetectSquares) {.exportc.} = discard

different-ways-to-add-parentheses.nim

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sequtils, strutils
2+
3+
proc malloc(size: csize_t): pointer {.importc.}
4+
5+
proc diffWaysToCompute(s: cstring, returnSize: ptr cint): ptr UncheckedArray[cint] {.exportc.} =
6+
var
7+
i = 0
8+
nums = newSeq[int]()
9+
ops = newSeq[char]()
10+
while true:
11+
var x = 0
12+
while (x = x*10+s[i].ord-'0'.ord; inc i; s[i].isDigit):
13+
discard
14+
nums.add(x)
15+
if s[i] == 0.char: break
16+
ops.add(s[i])
17+
inc i
18+
let n = nums.len
19+
var s = newSeqWith(n, newSeqWith(n, newSeq[int]()))
20+
for i in 0..<n:
21+
s[i][i].add(nums[i])
22+
for i in countdown(n-2, 0):
23+
for j in i+1..<n:
24+
for k in i..<j:
25+
for x in s[i][k]:
26+
for y in s[k+1][j]:
27+
s[i][j].add(case ops[k]
28+
of '+': x+y
29+
of '-': x-y
30+
else: x*y)
31+
32+
returnSize[] = cast[cint](s[0][n-1].len)
33+
result = cast[ptr UncheckedArray[cint]](malloc(cast[csize_t](cint.sizeof*returnSize[])))
34+
for i in 0..<s[0][n-1].len:
35+
result[i] = cast[cint](s[0][n-1][i])
+24-33
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,47 @@
11
// Longest Subsequence Repeated k Times
2-
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
2+
#define FOR(i, a, b) for (long i = (a); i < (b); i++)
33
#define REP(i, n) FOR(i, 0, n)
4-
#define ROF(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (b); --i >= (a); )
5-
using pii = pair<int, int>;
4+
#define ROF(i, a, b) for (long i = (b); --i >= (a); )
65

76
class Solution {
8-
vector<pii> ab;
97
vector<array<int, 26>> nxt;
108
string cur, ans;
11-
int k;
12-
bool dfs(int st, int i) {
13-
bool ok = 0;
14-
for (auto &c: ab) {
15-
if (c.second == 0) continue;
16-
int j = nxt[i][c.first];
9+
int freq[26] = {}, k;
10+
void dfs(int l, int i) {
11+
ROF(c, 0, 26) {
12+
if (freq[c] < k) continue;
13+
int j = nxt[i][c];
1714
if (j < 0) continue;
18-
c.second--;
19-
cur.push_back('a'+c.first);
20-
ok |= dfs(st+1, j);
21-
cur.pop_back();
22-
c.second++;
15+
16+
freq[c] -= k;
17+
cur[l-1] = 'a'+c;
18+
REP(_, k-1)
19+
REP(x, l) {
20+
j = nxt[j][cur[x]-'a'];
21+
if (j < 0) goto out;
22+
}
23+
if (l > ans.size())
24+
ans = cur.substr(0, l);
25+
dfs(l+1, nxt[i][c]);
26+
out:
27+
freq[c] += k;
2328
}
24-
if (ok)
25-
return true;
26-
if (st <= ans.size())
27-
return false;
28-
REP(_, k-1)
29-
for (char c: cur) {
30-
i = nxt[i][c-'a'];
31-
if (i < 0) return false;
32-
}
33-
ans = cur;
34-
return true;
3529
}
3630
public:
3731
string longestSubsequenceRepeatedK(string s, int k) {
38-
this->k = k;
39-
int freq[26] = {}, n = s.size();
32+
int n = s.size();
4033
for (char c: s)
4134
freq[c-'a']++;
42-
ROF(c, 0, 26)
43-
if (freq[c] >= k)
44-
ab.emplace_back(c, freq[c]/k);
45-
4635
nxt.resize(n+1);
4736
REP(c, 26)
4837
nxt[n][c] = -1;
4938
ROF(i, 0, n) {
5039
nxt[i] = nxt[i+1];
5140
nxt[i][s[i]-'a'] = i+1;
5241
}
53-
dfs(0, 0);
42+
this->k = k;
43+
cur.resize(n/k);
44+
dfs(1, 0);
5445
return ans;
5546
}
5647
};

0 commit comments

Comments
 (0)