답안 #887065

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
887065 2023-12-13T15:45:02 Z chanhchuong123 Trener (COCI20_trener) C++14
110 / 110
150 ms 3740 KB
#include <bits/stdc++.h>
using namespace std;
#define task "MAKELIST"
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

const int MOD = 1e9 + 7;
int n, k;
int dp[55][1505];
int hh[55][1505][3];

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

	if (fopen(task".inp", "r")) {
		freopen(task".inp", "r",  stdin);
		freopen(task".out", "w", stdout);
	}

    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= k; ++j) {
            string s;
            cin >> s;
            for (int l = 0, r = max(0, i - 2); r < i; ++l, ++r) {
                int hashing = 0;
                for (int k = l; k <= r; ++k) {
                    hashing = 29LL * hashing % MOD;
                    hashing += s[k] - 'a' + 1;
                    hashing %= MOD;
                }
                if (i == 1) {
                    hh[i][j][2] = hashing;
                    continue;
                }
                hh[i][j][l] = hashing;
                if (l == 0) {
                    hashing = 29LL * hashing % MOD;
                    hashing += s.back() - 'a' + 1;
                    hashing %= MOD;
                    hh[i][j][2] = hashing;
                }
            }
        }
    }

    for (int j = 1; j <= k; ++j) {
        dp[1][j] = 1;
    }
    for (int i = 1; i < n; ++i) {
        for (int a = 1; a <= k; ++a) if (dp[i][a]) {
            for (int b = 1; b <= k; ++b) if (hh[i][a][2] == hh[i + 1][b][0] || hh[i][a][2] == hh[i + 1][b][1]) {
                dp[i + 1][b] += dp[i][a];
                if (dp[i + 1][b] >= MOD)
                    dp[i + 1][b] -= MOD;
            }
        }
    }

    int res = 0;
    for (int j = 1; j <= k; ++j) {
        res += dp[n][j];
        if (res >= MOD)
            res -= MOD;
    }
    cout << res;

	return 0;
}

Compilation message

trener.cpp: In function 'int main()':
trener.cpp:16:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |   freopen(task".inp", "r",  stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
trener.cpp:17:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 860 KB Output is correct
2 Correct 4 ms 860 KB Output is correct
3 Correct 4 ms 992 KB Output is correct
4 Correct 4 ms 860 KB Output is correct
5 Correct 4 ms 860 KB Output is correct
6 Correct 4 ms 860 KB Output is correct
7 Correct 4 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 4 ms 860 KB Output is correct
6 Correct 4 ms 860 KB Output is correct
7 Correct 4 ms 992 KB Output is correct
8 Correct 4 ms 860 KB Output is correct
9 Correct 4 ms 860 KB Output is correct
10 Correct 4 ms 860 KB Output is correct
11 Correct 4 ms 860 KB Output is correct
12 Correct 147 ms 3740 KB Output is correct
13 Correct 140 ms 3408 KB Output is correct
14 Correct 136 ms 3412 KB Output is correct
15 Correct 137 ms 3412 KB Output is correct
16 Correct 149 ms 3412 KB Output is correct
17 Correct 148 ms 3568 KB Output is correct
18 Correct 148 ms 3412 KB Output is correct
19 Correct 147 ms 3412 KB Output is correct
20 Correct 148 ms 3560 KB Output is correct
21 Correct 147 ms 3412 KB Output is correct
22 Correct 150 ms 3412 KB Output is correct