/* 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] -= H[i][j] >= MOD ? MOD : 0;
}
}
}
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
trener.cpp: In function 'int main()':
trener.cpp:59:15: warning: unused variable 'aa' [-Wunused-variable]
59 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
127832 KB |
Output is correct |
2 |
Correct |
24 ms |
128088 KB |
Output is correct |
3 |
Correct |
28 ms |
128080 KB |
Output is correct |
4 |
Correct |
25 ms |
127836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
129872 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
127832 KB |
Output is correct |
2 |
Correct |
24 ms |
128088 KB |
Output is correct |
3 |
Correct |
28 ms |
128080 KB |
Output is correct |
4 |
Correct |
25 ms |
127836 KB |
Output is correct |
5 |
Incorrect |
31 ms |
129872 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |