This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define eb emplace_back
#define pb push_back
#define ppb pop_back
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define cln(a,s) memset((a),0,sizeof((a)[0])*(s))
#define all(x) (x).begin() , (x).end()
#define fi first
#define se second
#define int long long
using pii = pair<int,int>;
using ll = long long;
const int maxn = 55;
const int maxk = 1505;
const int inf = 1e9;
const int mod = 1e9+7;
const int hmod = 1e9+9;
int n,k;
string a[maxn][maxk];
int dp[maxn][maxk],hsh[maxn][maxk][maxn];
//a[i][j] 1 indexli
//stringler 0 indexli
//dp 1 indexli
//hsh[][] 1 indexli , stringler 0 indexli olduğu için hsh[][] 0 indexli
int32_t main () {
//freopen("in","r",stdin); freopen("out","w",stdout);
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
cin >> n >> k;
for(int i=1;i<=n;i++)
for(int j=1;j<=k;j++) {
cin >> a[i][j];
string tmp = a[i][j];
hsh[i][j][0] = tmp[0]-'a'+1;
int pow = 1;
for(int m=1;m<i;m++) {
pow *= 31;
pow %= hmod;
hsh[i][j][m] = hsh[i][j][m-1] + pow*(tmp[m]-'a'+1);
hsh[i][j][m] %= hmod;
}
//cout << i << " " << j << " " << hsh[i][j][i-1] << endl;
}
for(int i=1;i<=k;i++) dp[1][i] = 1;
for(int i=2;i<=n;i++)
for(int j=1;j<=k;j++) {
int h1 = hsh[i][j][i-2];
int h2 = hsh[i][j][i-1] - hsh[i][j][0];
//cout << a[i][j] << " " << h1 << " " << h2 << endl;
h2 += 2*hmod;
h2 %= hmod;
for(int m=1;m<=k;m++) {
if(hsh[i-1][m][i-2] == h1) {
dp[i][j] += dp[i-1][m];
dp[i][j] %= mod;
}
else if((31*hsh[i-1][m][i-2])%hmod == h2) {
dp[i][j] += dp[i-1][m];
dp[i][j] %= mod;
}
}
}
int ans = 0;
for(int i=1;i<=k;i++) {
ans += dp[n][i];
ans %= mod;
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |