Submission #223265

# Submission time Handle Problem Language Result Execution time Memory
223265 2020-04-15T06:28:53 Z dantoh000 Trener (COCI20_trener) C++14
55 / 110
2000 ms 1656 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
int n,k;
ll KEY = 93993939;
ll f[55];
int v[26];
ll HASH(string s){
    ll res = 0;
    for (int i = 0; i < s.size(); i++){
        res = ((res*KEY) + v[s[i]-'a'])%mod;
    }
    return res;
}
int dp[2][1505];
ll a[55][1505];
inline bool can(ll a, ll b, int len){
    for (int i = 0; i < 26; i++){
        if ((a*KEY + v[i])%mod == b || (a + v[i]*f[len])%mod == b) return true;
    }
    return false;
}
int main(){
    iota(v,v+26,0);
    random_shuffle(v,v+26);
    //for (int i = 0; i < 26; i++) printf("%d ",v[i]);
    f[0] = 1;
    for (int i = 1; i <= 50; i++){
        f[i] = (f[i-1]*KEY)%mod;
    }
    scanf("%d%d",&n,&k);
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= k; j++){
            string s;
            cin >> s;
            a[i][j] = HASH(s);
        }
    }
    for (int i = 1; i <= k; i++) dp[n&1][i] = 1;
    for (int i = n-1; i >= 1; i--){
        for (int j = 1; j <= k; j++){
            dp[i&1][j] = 0;
            for (int j2 = 1; j2 <= k; j2++){
                if (can(a[i][j],a[i+1][j2],i)){
                    dp[i&1][j] += dp[1-i&1][j2];
                    dp[i&1][j] %= mod;
                }
            }
        }
    }
    int sum = 0;
    for (int i = 1; i <= k; i++) {
        sum += dp[1][i];
        sum %= mod;
    }
    printf("%d",sum);
}

Compilation message

trener.cpp: In function 'll HASH(std::__cxx11::string)':
trener.cpp:11:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < s.size(); i++){
                     ~~^~~~~~~~~~
trener.cpp: In function 'int main()':
trener.cpp:46:39: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
                     dp[i&1][j] += dp[1-i&1][j2];
                                      ~^~
trener.cpp:32:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&k);
     ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 640 KB Output is correct
2 Correct 59 ms 640 KB Output is correct
3 Correct 59 ms 640 KB Output is correct
4 Correct 18 ms 640 KB Output is correct
5 Correct 58 ms 640 KB Output is correct
6 Correct 63 ms 640 KB Output is correct
7 Correct 57 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 61 ms 640 KB Output is correct
6 Correct 59 ms 640 KB Output is correct
7 Correct 59 ms 640 KB Output is correct
8 Correct 18 ms 640 KB Output is correct
9 Correct 58 ms 640 KB Output is correct
10 Correct 63 ms 640 KB Output is correct
11 Correct 57 ms 640 KB Output is correct
12 Execution timed out 2090 ms 1656 KB Time limit exceeded
13 Halted 0 ms 0 KB -