This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 2000, M = 1e5+10, K = 18;
ll n, k, dp[N][N], H[N][N], p[N];
string s[N][N];
bool subseq(int i, int j, int i1, int j1){
ll h = H[i][j];
ll h1 = H[i1][j1];
if((h * p[1]) % MOD == (h1 - (s[i1][j1][0] - 'a' + 1) + MOD) % MOD) return 1;
if(h == (((h1 - (s[i1][j1][i1 - 1] - 'a' + 1) * p[i1 - 1]) % MOD) + MOD) % MOD) return 1;
return 0;
}
void solve(){
p[0] = 1;
for(int i = 1; i < N; ++i) p[i] = (p[i-1] * 37) % MOD;
cin >> n >> k;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= k; ++j){
cin >> s[i][j];
H[i][j] = 0;
for(int l = 0; l < i; ++l){
H[i][j] += p[l] * (s[i][j][l] - 'a' + 1);
H[i][j] %= MOD;
}
}
}
for(int i = 1; i <= n; ++i) for(int j = 0; j <= k; ++j) dp[i][j] = (n == i);
for(int i = n - 1; i >= 1; --i){
for(int j = 1; j <= k; ++j){
for(int to = 1; to <= k; ++to){
if(subseq(i, j, i + 1, to)){
dp[i][j] += dp[i + 1][to];
dp[i][j] -= dp[i][j] >= MOD ? MOD : 0;
}
}
}
}
ll ans = 0;
for(int i = 1; i <= k; ++i) (ans+=dp[1][i])%=MOD;
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// cin >> tt;
while(tt--){
solve();
// en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
trener.cpp: In function 'int main()':
trener.cpp:59:15: warning: unused variable 'aa' [-Wunused-variable]
59 | int tt = 1, aa;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |