File size: 2,676 Bytes
5fed0fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static const int DIRS[2][2] = {{0,1},{1,0}};

int main(int argc, char* argv[]) {
    registerTestlibCmd(argc, argv);

    // Read input
    long long N = inf.readLong();
    long long M = inf.readLong();
    vector<string> s(M);
    for (long long i = 0; i < M; i++) {
        s[i] = inf.readToken();
    }

    // Read output matrix
    vector<string> out(N);
    for (long long i = 0; i < N; i++) {
        out[i] = ouf.readToken();
    }
    if (!ouf.seekEof()) {
        quitf(_wa, "Too many lines");
    }

    // Validate output matrix
    long long dotCount = 0;
    for (long long i = 0; i < N; i++) {
        if ((long long)out[i].size() != N) {
            quitf(_wa, "illegal length: %lld", (long long)out[i].size());
        }
        for (long long j = 0; j < N; j++) {
            char c = out[i][j];
            if (!((c >= 'A' && c <= 'H') || c == '.')) {
                quitf(_wa, "illegal char: %c", c);
            }
            if (c == '.') dotCount++;
        }
    }

    auto is_substring = [&](const vector<string>& a, const string& b, long long i, long long j, int d) -> bool {
        int di = DIRS[d][0], dj = DIRS[d][1];
        for (long long k = 0; k < (long long)b.size(); k++) {
            long long ii = (i + di * k) % N;
            long long jj = (j + dj * k) % N;
            if (a[ii][jj] != b[(size_t)k]) return false;
        }
        return true;
    };

    // Compute c = number of strings that appear as a subsequence
    long long c = 0;
    for (long long idx = 0; idx < M; idx++) {
        bool used = false;
        for (long long i = 0; i < N && !used; i++) {
            for (long long j = 0; j < N && !used; j++) {
                for (int d = 0; d < 2 && !used; d++) {
                    if (is_substring(out, s[idx], i, j, d)) {
                        used = true;
                    }
                }
            }
        }
        if (used) c++;
    }

    // Compute score
    double scoreD;
    if (c < M) {
        scoreD = 1e8 * (double)c / (double)M;
    } else {
        double twoNN = 2.0 * (double)N * (double)N;
        scoreD = 1e8 * twoNN / (twoNN - (double)dotCount);
    }
    long long score = llround(scoreD);
    long long baseline_value = ans.readLong();
    long long best_value = ans.readLong();

    double score_ratio = max(0.0, min(1.0, (double)(score - baseline_value) / (best_value - baseline_value)));
    double unbounded_ratio = max(0.0, (double)(score - baseline_value) / (best_value - baseline_value));
    quitp(score_ratio, "Value: %lld. Ratio: %.4f, RatioUnbounded: %.4f", score, score_ratio, unbounded_ratio);
}