Submission #246319

# Submission time Handle Problem Language Result Execution time Memory
246319 2020-07-08T16:07:17 Z alrad Trener (COCI20_trener) C++17
110 / 110
796 ms 3576 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()

const long long P = 31;
const long long HashMod = 1e9 + 123;
const int MOD = 1e9 + 7;

int main() {
   #ifdef judge
      ifstream cin("input.txt");
   #endif // judge
   ios_base :: sync_with_stdio(0);
   cin.tie(0) , cout.tie(0);
   int n , k;
   cin >> n >> k;
   vector<long long> pow;
   pow.push_back(1LL);
   while ((int)pow.size() <= n) {
      pow.push_back(1LL * pow.back() * P % MOD);
   }
   auto getHash = [&](string s) {
      int res = 0;
      for (int i = 0; i < (int)s.size(); i++) {
         res += ((1LL * s[i] * pow[i]) % MOD);
         res %= MOD;
      }
      return res;
   };
   int full[n][k] , pref[n][k] , suf[n][k];
   for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
         string x;
         cin >> x;
         full[i][j] = getHash(x);
         if (i > 0) {
            pref[i][j] = getHash(x.substr(0 , i));
            suf[i][j] = getHash(x.substr(1 , i));
         }
      }
   }
   vector<vector<int> > dp(n , vector<int>(k , 0));
   for (int j = 0; j < k; j++) {
      dp[0][j] = 1;
   }
   for (int i = 1; i < n; i++) {
      for (int cur = 0; cur < k; cur++) {
         for (int prev = 0; prev < k; prev++) {
            if (full[i - 1][prev] == pref[i][cur] || full[i - 1][prev] == suf[i][cur]) {
               dp[i][cur] += dp[i - 1][prev];
               dp[i][cur] %= MOD;
            }
         }
      }
   }
   int ans = 0;
   for (int j = 0; j < k; j++) {
      ans += dp[n - 1][j];
      ans %= MOD;
   }
   cout << ans << '\n';
   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 384 KB Output is correct
2 Correct 9 ms 384 KB Output is correct
3 Correct 8 ms 384 KB Output is correct
4 Correct 11 ms 384 KB Output is correct
5 Correct 9 ms 384 KB Output is correct
6 Correct 8 ms 384 KB Output is correct
7 Correct 11 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 8 ms 384 KB Output is correct
6 Correct 9 ms 384 KB Output is correct
7 Correct 8 ms 384 KB Output is correct
8 Correct 11 ms 384 KB Output is correct
9 Correct 9 ms 384 KB Output is correct
10 Correct 8 ms 384 KB Output is correct
11 Correct 11 ms 384 KB Output is correct
12 Correct 174 ms 1584 KB Output is correct
13 Correct 177 ms 1656 KB Output is correct
14 Correct 174 ms 1528 KB Output is correct
15 Correct 178 ms 1656 KB Output is correct
16 Correct 796 ms 1528 KB Output is correct
17 Correct 205 ms 1528 KB Output is correct
18 Correct 201 ms 3448 KB Output is correct
19 Correct 206 ms 3576 KB Output is correct
20 Correct 202 ms 3576 KB Output is correct
21 Correct 201 ms 3448 KB Output is correct
22 Correct 792 ms 3528 KB Output is correct