= 0); } // 是否在0~179度?点在上半边,极角排序用的
58 | P rot(db an){ return {x*cos(an)-y*sin(an),x*sin(an) + y*cos(an)}; } // 逆时针旋转an度
59 | };
60 |
61 | int type(P o1,db r1,P o2,db r2){ // 两个圆的关系,0:内含,1:内切,2:相交,3:外切,4:相离
62 | db d = o1.distTo(o2);
63 | if(cmp(d,r1+r2) == 1) return 4;
64 | if(cmp(d,r1+r2) == 0) return 3;
65 | if(cmp(d,abs(r1-r2)) == 1) return 2;
66 | if(cmp(d,abs(r1-r2)) == 0) return 1;
67 | return 0;
68 | }
69 |
70 | vector isCC(P o1, db r1, P o2, db r2) { //need to check whether two circles are the same
71 | db d = o1.distTo(o2);
72 | if (cmp(d, r1 + r2) == 1) return {};
73 | if (cmp(d,abs(r1-r2))==-1) return {};
74 | d = min(d, r1 + r2);
75 | db y = (r1 * r1 + d * d - r2 * r2) / (2 * d), x = sqrt(r1 * r1 - y * y);
76 | P dr = (o2 - o1).unit();
77 | P q1 = o1 + dr * y, q2 = dr.rot90() * x;
78 | return {q1-q2,q1+q2};//along circle 1
79 | }
80 |
81 | int main(void) {
82 | ios::sync_with_stdio(false);
83 | cin.tie(0); cout.tie(0);
84 | P p1, p2;
85 | int r1, r2;
86 | p1.read();
87 | cin >> r1;
88 | p2.read();
89 | cin >> r2;
90 | int sign = type(p1, r1, p2, r2);
91 | assert(sign != 4);
92 | if (sign == 0 || sign == 1){
93 | if (r1 < r2)
94 | cout << fixed << setprecision(10) << p1.x << ' ' << p1.y << '\n';
95 | else
96 | cout << fixed << setprecision(10) << p2.x << ' ' << p2.y << '\n';
97 | }else{
98 | auto good = isCC(p1, r1, p2, r2);
99 | cout << fixed << setprecision(10) << good.front().x << ' ' << good.front().y << '\n';
100 | }
101 |
102 | return 0;
103 | }
104 |
--------------------------------------------------------------------------------
/code/cf/gym/101522/h/testI1.txt:
--------------------------------------------------------------------------------
1 | 0 0 3
2 |
3 | 3 4 3
4 |
--------------------------------------------------------------------------------
/code/cf/gym/101522/h/testI2.txt:
--------------------------------------------------------------------------------
1 | -7 -9 3
2 |
3 | -4 -4 5
4 |
--------------------------------------------------------------------------------
/code/cf/gym/101522/h/testO1.txt:
--------------------------------------------------------------------------------
1 | 1.5 2.5
2 |
--------------------------------------------------------------------------------
/code/cf/gym/101522/h/testO2.txt:
--------------------------------------------------------------------------------
1 | -6 -7
2 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/a/a.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 13:01:52
4 | */
5 |
6 | #include
7 | using namespace std;
8 | using LL = long long;
9 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
10 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
11 |
12 | #ifndef ONLINE_JUDGE
13 | #include "debug.h"
14 | #else
15 | #define debug(x...)
16 | #endif
17 |
18 | int main(void) {
19 | ios::sync_with_stdio(false);
20 | cin.tie(0); cout.tie(0);
21 | int n;
22 | cin >> n;
23 | bool ok = false, haveC = false, haveV = false;
24 | while(n--){
25 | string s;
26 | cin >> s;
27 | if (s.size() == 4)
28 | ok = true;
29 | else if (s.size() == 6)
30 | ok = false;
31 | else if (ok && s[0] == 'C')
32 | haveC = true;
33 | else if (ok && s[0] == 'V' && haveC)
34 | haveV = true;
35 | }
36 | if (haveV)
37 | cout << "Yes" << '\n';
38 | else
39 | cout << "No" << '\n';
40 |
41 | return 0;
42 | }
43 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/b/b.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 13:20:34
4 | */
5 |
6 | #include
7 | #include
8 | using namespace std;
9 | using LL = long long;
10 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
11 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
12 |
13 | #ifndef ONLINE_JUDGE
14 | #include "debug.h"
15 | #else
16 | #define debug(x...)
17 | #endif
18 |
19 | const int N = 1e5 + 8;
20 | int cnt[N], t, k;
21 |
22 | int main(void) {
23 | ios::sync_with_stdio(false);
24 | cin.tie(0); cout.tie(0);
25 | cin >> t >> k;
26 | for(int i = 1; i <= t; ++ i){
27 | int l, r;
28 | cin >> l >> r;
29 | cnt[l] ++;
30 | cnt[r + 1] --;
31 | }
32 | vector> ans;
33 | int st = 0;
34 | bool ok = true;
35 | int sum = 0;
36 | for(int i = 0; i <= 120; ++ i){
37 | sum += cnt[i];
38 | if (ok && sum >= k){
39 | ans.push_back({st, i - 1});
40 | ok = false;
41 | }else if (sum < k){
42 | if (!ok)
43 | st = i;
44 | ok = true;
45 | }
46 | }
47 | if (ok)
48 | ans.push_back({st, 120});
49 | cout << ans.size() << '\n';
50 | for(auto &i : ans)
51 | cout << i.first << '-' << i.second << '\n';
52 |
53 | return 0;
54 | }
55 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/c/c.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 13:05:54
4 | */
5 |
6 | #include
7 | using namespace std;
8 | using LL = long long;
9 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
10 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
11 |
12 | #ifndef ONLINE_JUDGE
13 | #include "debug.h"
14 | #else
15 | #define debug(x...)
16 | #endif
17 |
18 | int main(void) {
19 | ios::sync_with_stdio(false);
20 | cin.tie(0); cout.tie(0);
21 | int s, t;
22 | cin >> s >> t;
23 | int ans = 0;
24 | FOR(i, 0, s + 1)
25 | FOR(j, i, s + 1)
26 | FOR(k, j, s + 1)
27 | ans += (i + j + k <= s && 1ll * i * j * k <= t);
28 | cout << ans << '\n';
29 |
30 |
31 | return 0;
32 | }
33 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/d/d.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 13:42:56
4 | */
5 |
6 | #include
7 | using namespace std;
8 | using LL = long long;
9 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
10 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
11 |
12 | #ifndef ONLINE_JUDGE
13 | #include "debug.h"
14 | #else
15 | #define debug(x...)
16 | #endif
17 |
18 | int main(void) {
19 | ios::sync_with_stdio(false);
20 | cin.tie(0); cout.tie(0);
21 | cout << "5 Mazige" << '\n';
22 |
23 | return 0;
24 | }
25 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/e/e.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 13:43:13
4 | */
5 |
6 | #include
7 | using namespace std;
8 | using LL = long long;
9 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
10 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
11 |
12 | #ifndef ONLINE_JUDGE
13 | #include "debug.h"
14 | #else
15 | #define debug(x...)
16 | #endif
17 |
18 | int main(void) {
19 | ios::sync_with_stdio(false);
20 | cin.tie(0); cout.tie(0);
21 | int t;
22 | cin >> t;
23 | while(t--){
24 | string n, k;
25 | cin >> n >> k;
26 | cout << n.back() << '\n';
27 | }
28 |
29 | return 0;
30 | }
31 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/f/f.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 13:45:37
4 | */
5 |
6 | #include
7 | using namespace std;
8 | using LL = long long;
9 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
10 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
11 |
12 | #ifndef ONLINE_JUDGE
13 | #include "debug.h"
14 | #else
15 | #define debug(x...)
16 | #endif
17 |
18 | const LL inf = 1e18 + 7;
19 |
20 | int main(void) {
21 | ios::sync_with_stdio(false);
22 | cin.tie(0); cout.tie(0);
23 | int n;
24 | cin >> n;
25 | map cnt, ans;
26 | for(int i = 0; i < n; ++ i){
27 | LL x;
28 | cin >> x;
29 | LL cc = 0;
30 | while(x >= 3){
31 | ans[x] += cc;
32 | cnt[x] ++;
33 | ++ cc;
34 | x /= 3;
35 | }
36 | }
37 | LL val = inf;
38 | for(auto &i : cnt)
39 | if (i.second == n)
40 | val = min(val, ans[i.first]);
41 | if (val == inf)
42 | cout << "Lose" << '\n';
43 | else
44 | cout << val << '\n';
45 |
46 | return 0;
47 | }
48 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/g/g.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 13:55:33
4 | */
5 |
6 | #include
7 | using namespace std;
8 | using LL = long long;
9 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
10 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
11 |
12 | #ifndef ONLINE_JUDGE
13 | #include "debug.h"
14 | #else
15 | #define debug(x...)
16 | #endif
17 |
18 | const int N = 200;
19 | int dp[N][N];
20 |
21 | int solve(int x, int y){
22 | if (dp[x][y] != -1)
23 | return dp[x][y];
24 | int v1 = 1, v2 = 1, v3 = 1;
25 | if (x > 0){
26 | v1 = solve(x - 1, y);
27 | v2 = solve(x - 1, y + 1);
28 | }
29 | if (y > 0){
30 | v3 = solve(x, y - 1);
31 | }
32 | if (v1 == 0 || v2 == 0 || v3 == 0)
33 | dp[x][y] = 1;
34 | else
35 | dp[x][y] = 0;
36 | return dp[x][y];
37 | }
38 |
39 | int main(void) {
40 | ios::sync_with_stdio(false);
41 | cin.tie(0); cout.tie(0);
42 | // memset(dp, -1, sizeof(dp));
43 | // dp[0][0] = 0;
44 | // for(int i = 0; i < N; ++ i)
45 | // for(int j = 0; j < N; ++ j){
46 | // solve(i, j);
47 | // }
48 | // for(int i = 0; i < N; ++ i){
49 | // debug(i * 2, dp[i][0]);
50 | // debug(i * 2 + 1, dp[i][1]);
51 | // }
52 | int t;
53 | cin >> t;
54 | while(t--){
55 | LL n;
56 | cin >> n;
57 | if ((n & 1) || ((n / 2) & 1)){
58 | cout << "Mazige" << '\n';
59 | }else{
60 | cout << "Not Mazige" << '\n';
61 | }
62 | }
63 |
64 | return 0;
65 | }
66 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/h/h.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 14:15:51
4 | */
5 |
6 | #include
7 | #include
8 | using namespace std;
9 | using LL = long long;
10 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
11 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
12 |
13 | #ifndef ONLINE_JUDGE
14 | #include "debug.h"
15 | #else
16 | #define debug(x...)
17 | #endif
18 |
19 | const LL mo = 1e9 + 7;
20 |
21 | int main(void) {
22 | ios::sync_with_stdio(false);
23 | cin.tie(0); cout.tie(0);
24 | int n, k;
25 | cin >> n >> k;
26 | vector a(n);
27 | for(auto &i : a){
28 | cin >> i;
29 | }
30 | sort(a.begin(), a.end(), [](LL a, LL b){
31 | return abs(a) > abs(b);
32 | });
33 | int kp = -1, kn = -1;
34 | int cnt = 0;
35 | for(int i = 0; i < k; ++ i){
36 | cnt ^= (a[i] < 0);
37 | if (a[i] > 0)
38 | kp = i;
39 | else if (a[i] < 0)
40 | kn = i;
41 | }
42 | // first k
43 | if (cnt == 0){
44 | LL ans = 1;
45 | for(int i = 0; i < k; ++ i)
46 | ans = ans * abs(a[i]) % mo;
47 | cout << ans << '\n';
48 | return 0;
49 | }
50 | int kkp = -1, kkn = -1;
51 | for(int i = k; i < n; ++ i){
52 | if (kkp == -1 && a[i] > 0)
53 | kkp = i;
54 | if (kkn == -1 && a[i] < 0)
55 | kkn = i;
56 | }
57 | // swap biggest last positive with smallest k neg
58 | if (kkp != -1 && (kkp < kkn || kkn == -1 || kp == -1)){
59 | LL ans = 1;
60 | for(int i = 0; i < k; ++ i){
61 | if (i == kn)
62 | continue;
63 | ans = ans * abs(a[i]) % mo;
64 | }
65 | ans = ans * a[kkp] % mo;
66 | cout << ans << '\n';
67 | return 0;
68 | }
69 | // swap biggest last negative with smallest k positive
70 | if (kkn != -1 && kp != -1){
71 | LL ans = 1;
72 | for(int i = 0; i < k; ++ i){
73 | if (i == kp)
74 | continue;
75 | ans = ans * abs(a[i]) % mo;
76 | }
77 | ans = ans * abs(a[kkn]) % mo;
78 | cout << ans << '\n';
79 | return 0;
80 | }
81 | // ans < 0, last k
82 | LL ans = 1;
83 | for(int i = n - 1; i > n - 1 - k; -- i){
84 | ans = ans * abs(a[i]) % mo;
85 | }
86 | cout << -ans << '\n';
87 |
88 | return 0;
89 | }
90 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/i/i.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 14:48:25
4 | */
5 |
6 | #include
7 | #include
8 | using namespace std;
9 | using LL = long long;
10 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
11 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
12 |
13 | #ifndef ONLINE_JUDGE
14 | #include "debug.h"
15 | #else
16 | #define debug(x...)
17 | #endif
18 |
19 | int main(void) {
20 | ios::sync_with_stdio(false);
21 | cin.tie(0); cout.tie(0);
22 | int n, t;
23 | cin >> n >> t;
24 | vector a(n);
25 | for(auto &i : a)
26 | cin >> i;
27 | while(t--){
28 | int l, r;
29 | cin >> l >> r;
30 | -- l;
31 | -- r;
32 | int ans = 0;
33 | for(int i = l; i <= r; ++ i)
34 | ans += (a[i] >= a[l]);
35 | cout << ans << '\n';
36 | }
37 |
38 | return 0;
39 | }
40 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/k/k.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 16:39:00
4 | */
5 |
6 | #include
7 | #include
8 | using namespace std;
9 | using LL = long long;
10 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
11 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
12 |
13 | #ifndef ONLINE_JUDGE
14 | #include "debug.h"
15 | #else
16 | #define debug(x...)
17 | #endif
18 |
19 | const LL inf = 1e18;
20 |
21 | int main(void) {
22 | ios::sync_with_stdio(false);
23 | cin.tie(0); cout.tie(0);
24 | int n;
25 | cin >> n;
26 | vector dp(3001, inf);
27 | dp[0] = 0;
28 | vector> f(n);
29 | for(auto &i : f)
30 | cin >> i.first >> i.second;
31 | sort(f.begin(), f.end(), [](const pair& a, const pair& b){
32 | return a.first < b.first;
33 | });
34 | for(int i = 0; i < n; ++ i){
35 | LL a;
36 | int b;
37 | a = f[i].first;
38 | b = f[i].second;
39 | for(int j = 3000; j >= b; -- j){
40 | if (a > dp[j - b])
41 | dp[j] = min(dp[j], dp[j - b] + a);
42 | }
43 | }
44 | int ans = 0;
45 | for(int i = 3000; i >= 0; -- i)
46 | if (dp[i] != inf){
47 | ans = i;
48 | break;
49 | }
50 | cout << ans << '\n';
51 | return 0;
52 | }
53 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/l/l.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 15:02:23
4 | */
5 |
6 | #include
7 | using namespace std;
8 | using LL = long long;
9 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
10 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
11 |
12 | #ifndef ONLINE_JUDGE
13 | #include "debug.h"
14 | #else
15 | #define debug(x...)
16 | #endif
17 |
18 | const LL inf = 1e18;
19 |
20 | int main(void) {
21 | ios::sync_with_stdio(false);
22 | cin.tie(0); cout.tie(0);
23 | LL n, s;
24 | cin >> n >> s;
25 | LL ans = inf;
26 | auto calc = [](LL x, int base){
27 | LL cnt = 0;
28 | while(x){
29 | cnt += x % base;
30 | x /= base;
31 | }
32 | return cnt;
33 | };
34 | for(int i = 2; i <= 100000; ++ i){
35 | if (calc(n, i) == s){
36 | ans = i;
37 | break;
38 | }
39 | }
40 | if (ans == inf){
41 | for(int i = 1; i <= 100000; ++ i){
42 | LL mod = s - i;
43 | if (n > mod && (n - mod) % i == 0){
44 | LL p = (n - mod) / i;
45 | if (p != 1){
46 | ans = min(ans, p);
47 | debug(p, i, mod);
48 | }
49 | }
50 | }
51 | }
52 |
53 | if (ans == inf)
54 | ans = -1;
55 | if (n == s && ans == -1)
56 | ans = n + 1;
57 | cout << ans << '\n';
58 |
59 | return 0;
60 | }
61 |
--------------------------------------------------------------------------------
/code/nowcoder/contests/49259/m/m.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Author: Lanly
3 | * Time: 2022-12-17 15:22:56
4 | */
5 |
6 | #include
7 | #include
8 | using namespace std;
9 | using LL = long long;
10 | #define FOR(i, x, y) for (decay::type i = (x), _##i = (y); i < _##i; ++i)
11 | #define FORD(i, x, y) for (decay::type i = (x), _##i = (y); i > _##i; --i)
12 |
13 | #ifndef ONLINE_JUDGE
14 | #include "debug.h"
15 | #else
16 | #define debug(x...)
17 | #endif
18 |
19 | const long double inf = 1e18;
20 | const long double pi = acos(-1);
21 | const long double sin18 = sin(18.0 / 180 * pi);
22 | const long double sin36 = sin(36.0 / 180 * pi);
23 | const long double sin72 = sin(72.0 / 180 * pi);
24 | const long double sin108 = sin(108.0 / 180 * pi);
25 | const long double cos18 = cos(18.0 / 180 * pi);
26 | const long double cos72 = cos(72.0 / 180 * pi);
27 | const long double cos108 = cos(108.0 / 180 * pi);
28 | const long double tan54 = tan(54.0 / 180 * pi);
29 | const long double eps = 1e-10;
30 |
31 | long double calcB(long double a){
32 | return 2.0 * a * sin18;
33 | }
34 |
35 | long double hoc(long double a){
36 | long double b = calcB(a);
37 | return a + a + b;
38 | }
39 |
40 | long double vec(long double a){
41 | return hoc(a) * cos18;
42 | }
43 |
44 | long double star(long double a){
45 | return 5.0 * 0.5 * a * a * sin36;
46 | }
47 |
48 | long double wubian(long double b){
49 | return 5.0 * 0.5 * b * b / 2 * tan54;
50 | }
51 |
52 |
53 | long double area(long double a){
54 | long double b = calcB(a);
55 | long double s1 = star(a);
56 | long double s2 = wubian(b);
57 | return s1 + s2;
58 | }
59 |
60 | long double solve(long double (*f)(long double), long double up){
61 | long double l = 0, r = up;
62 | while(l + eps < r){
63 | long double mid = (l + r) / 2;
64 | if (f(mid) <= up)
65 | l = mid;
66 | else
67 | r = mid;
68 | }
69 | return l;
70 | }
71 |
72 | long double cot(long double x){
73 | return tan(pi/2-x);
74 | }
75 |
76 | int main(void) {
77 | ios::sync_with_stdio(false);
78 | cin.tie(0); cout.tie(0);
79 | long double x, y;
80 | cin >> x >> y;
81 | cout << 5.0 / 8.0 * (x * x + y * y) * cot(pi / 5) << '\n';
82 | long double ff = min(x, y);
83 | long double a = sqrt(ff * ff / ((sin72 * sin72 + (1 + cos72) * (1 + cos72)) * sin108 * sin108 * 2 * (1 - cos108)));
84 | long double b = min(solve(hoc, x), solve(vec, y));
85 |
86 | cout << fixed << setprecision(10) << area(a) << ' ' << area(b) << '\n';
87 | // cout << fixed << setprecision(10) << area(a) << '\n';
88 |
89 | return 0;
90 | }
91 |
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | from parser.parser import Parser
2 | import sys
3 |
4 | url = ""
5 |
6 | # url = "https://atcoder.jp/contests/abc371/tasks"
7 | # url = "https://codeforces.com/contest/1857"
8 | # url = "https://codeforces.com/gym/101522"
9 | # url = "https://ac.nowcoder.com/acm/contest/49259"
10 |
11 | if not url and len(sys.argv) < 2:
12 | print(f"python3 {sys.argv[0]} ")
13 | exit(-1)
14 | elif not url:
15 | url = sys.argv[1]
16 |
17 | parser = Parser()
18 | parser.start(url)
19 |
--------------------------------------------------------------------------------
/parser/__init__.py:
--------------------------------------------------------------------------------
1 | # nothing
2 |
--------------------------------------------------------------------------------
/parser/atcoder.py:
--------------------------------------------------------------------------------
1 | from .base import BaseParser
2 |
3 | class Atcoder(BaseParser):
4 |
5 | @property
6 | def code_path(self) -> str:
7 | return "code/AtCoder"
8 |
9 | @property
10 | def request_cookie(self): # 有时候比赛剩下不会做,想写题解,但由于比赛还没结束,需要登录才能看题,可以F12获取登录cookie
11 | return {
12 | "REVEL_SESSION": ""
13 | }
14 |
15 | @property
16 | def name(self) -> str:
17 | return "atcoder"
18 |
19 | def _title_method(self) -> str:
20 | return self.soup.head.title.get_text().strip("Tasks - ")
21 |
22 | @property
23 | def problem_table(self) -> str:
24 | return "table"
25 |
26 | @property
27 | def contest_id(self) -> str:
28 | return self.url.split('/')[-2]
29 |
30 |
--------------------------------------------------------------------------------
/parser/base.py:
--------------------------------------------------------------------------------
1 | from abc import abstractmethod, abstractproperty
2 | import os
3 | import random
4 | from datetime import datetime
5 | import requests
6 | from bs4 import BeautifulSoup
7 | from urllib.parse import urljoin
8 |
9 | class BaseParser():
10 |
11 | def __init__(self) -> None:
12 | self._log = []
13 | self._title = ""
14 | self.problem_num = 0
15 |
16 | @abstractproperty
17 | def name(self) -> str: ...
18 | @abstractmethod
19 | def _title_method(self) -> str: ...
20 | @abstractmethod
21 | def problem_url(self, problem_id: str) -> str: ...
22 |
23 | @abstractproperty
24 | def contest_id(self) -> str: ...
25 | @abstractproperty
26 | def code_path(self) -> str: ...
27 | @abstractproperty
28 | def problem_table(self) -> str:
29 | return ""
30 |
31 | @property
32 | def request_header(self):
33 | return {}
34 |
35 | @property
36 | def request_cookie(self):
37 | return {}
38 |
39 | @property
40 | def abbr(self) -> str:
41 | return ""
42 |
43 | @property
44 | def template(self) -> str:
45 | return '''## [{} ({}{} {})]({})
46 | ### 题目大意
47 |
48 | <++>
49 |
50 | ### 解题思路
51 |
52 | <++>
53 |
54 |
55 | 神奇的代码
56 |
57 | ```cpp
58 | {}
59 | ```
60 |
61 |
62 |
63 | ***
64 | '''
65 |
66 | @property
67 | def header(self) -> str:
68 | return f'''---
69 | title: {self.title()}
70 | categories: 算法题解
71 | description: 我好菜啊
72 | tags:
73 | - {self.name}
74 | cover: /img/chino/vec/chino{random.randint(1,60)}.jpg
75 | katex: true
76 | date: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
77 | ---
78 |
79 | '''
80 |
81 | def brief(self, problem_num: int) -> str:
82 | head = '''
83 | 省流版
84 |
85 | '''
86 | tail = '''
87 |
88 |
89 |
90 | '''
91 |
92 | mid = '\n'.join([f"- {chr(65+i)}. <++>" for i in range(problem_num)])
93 |
94 | return head + mid + tail
95 |
96 | def get_problem(self) -> list:
97 | table_node = self.soup.find_all("table", class_=self.problem_table)
98 | trs = table_node[0].find_all("tr")
99 | self.problem_num = len(trs) - 1
100 | problems = []
101 | for i in range(self.problem_num):
102 | tr = trs[i + 1]
103 | tds = tr.find_all("td")
104 | a_tag = tds[1].find('a')
105 |
106 | problem_id = tds[0].get_text().strip()
107 | problem_name = problem_id + " - " + a_tag.get_text().strip()
108 | problem_url = urljoin(self.url, a_tag['href'])
109 |
110 | problems.append((problem_id, problem_name, problem_url))
111 |
112 | return problems
113 |
114 | def start(self, url: str):
115 | self.url = url
116 | res = self.request(self.url)
117 | res.raise_for_status()
118 | self.soup = BeautifulSoup(res.text, features="html.parser")
119 | self.log(self.header)
120 |
121 | problems = self.get_problem()
122 | self.problem_num = len(problems)
123 | self.log(self.brief(self.problem_num))
124 |
125 | print(f"find {self.problem_num} problem")
126 |
127 | for problem_id, problem_name, problem_url in problems:
128 | print(f"find [{problem_name}]")
129 | self.log(self.make_problem(problem_id, problem_name, problem_url))
130 | self.write()
131 |
132 | def request(self, url: str) -> requests.Response:
133 | return requests.get(url, headers=self.request_header, cookies=self.request_cookie)
134 |
135 | def title(self) -> str:
136 | if not self._title:
137 | self._title = self._title_method()
138 | print(f"title: {self._title}")
139 | return self._title
140 |
141 | def get_code(self, problem_id: str) -> str:
142 | p = problem_id.lower()
143 | fp = os.path.join(self.code_path, self.contest_id, p, f"{p}.cpp")
144 | if not os.path.exists(fp):
145 | print(f"{fp} not found!")
146 | return ""
147 | with open(fp, "r") as f:
148 | return f.read()
149 |
150 | def make_problem(self, problem_id: str, problem_name: str, problem_url: str) -> str:
151 | code = self.get_code(problem_id)
152 | return self.template.format(problem_name, self.abbr, self.contest_id, problem_id, problem_url, code)
153 |
154 | def write(self):
155 | with open(f"{self.title()}.md", "w") as f:
156 | f.write('\n'.join(self._log))
157 | print(f"Save in {self.title()}.md")
158 |
159 | def log(self, text:str):
160 | self._log.append(text)
161 |
--------------------------------------------------------------------------------
/parser/codeforces.py:
--------------------------------------------------------------------------------
1 | from .base import BaseParser
2 |
3 | class Codeforces(BaseParser):
4 |
5 | @property
6 | def code_path(self) -> str:
7 | return "code/cf/" + ("contest" if len(self.contest_id) <= 4 else "gym")
8 |
9 | @property
10 | def request_cookie(self): # 有时候有cloudflare,可以F12里的cookie获取这个字段绕过
11 | return {
12 | }
13 |
14 | @property
15 | def name(self) -> str:
16 | return "codeforces"
17 |
18 | @property
19 | def abbr(self) -> str:
20 | return "CF"
21 |
22 | @property
23 | def problem_table(self) -> str:
24 | return "problems"
25 |
26 | def _title_method(self) -> str:
27 | return self.soup.find(attrs="rtable").find(attrs="left").get_text().strip()
28 |
29 | @property
30 | def contest_id(self) -> str:
31 | return self.url.split('/')[-1]
32 |
33 |
--------------------------------------------------------------------------------
/parser/nowcoder.py:
--------------------------------------------------------------------------------
1 | from .base import BaseParser
2 |
3 | class Nowcoder(BaseParser):
4 |
5 | @property
6 | def code_path(self) -> str:
7 | return "code/nowcoder/contests"
8 |
9 | @property
10 | def name(self) -> str:
11 | return "nowcoder"
12 |
13 | @property
14 | def abbr(self) -> str:
15 | return "Nowcoder"
16 |
17 | def _title_method(self) -> str:
18 | return self.soup.head.title.get_text().split('_')[0]
19 |
20 | @property
21 | def problem_table(self) -> str:
22 | return "table-hover"
23 |
24 | @property
25 | def contest_id(self) -> str:
26 | return self.url.split('/')[-1]
27 |
28 | def get_problem(self):
29 | res = self.request(f"https://ac.nowcoder.com/acm/contest/problem-list?id={self.contest_id}").json()
30 | problems = []
31 | for problem in res['data']['data']:
32 | problem_id = problem['index']
33 | problem_name = problem_id + " - " + problem['title']
34 | problem_url = self.url + "/" + problem_id
35 | problems.append((problem_id, problem_name, problem_url))
36 |
37 | return problems
38 |
--------------------------------------------------------------------------------
/parser/parser.py:
--------------------------------------------------------------------------------
1 | from .atcoder import Atcoder
2 | from .codeforces import Codeforces
3 | from .nowcoder import Nowcoder
4 |
5 | class Parser():
6 |
7 | rules = {
8 | "atcoder" : Atcoder,
9 | "codeforces" : Codeforces,
10 | "nowcoder" : Nowcoder
11 | }
12 |
13 | def start(self, url: str):
14 | instance = None
15 | for key, value in self.rules.items():
16 | if key in url:
17 | instance = value()
18 | if instance is None:
19 | print("Unsupported url!")
20 | return
21 |
22 | instance.start(url)
23 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | beautifulsoup4==4.12.2
2 | requests==2.26.0
3 |
--------------------------------------------------------------------------------