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;
typedef long long ll;
const ll N = 55, K = 1505;
const ll base = 727, mod = 1e9 + 7;
ll n, k;
map<string, ll> cnt[N], dp[N];
int main(){
cin >> n >> k;
for (ll i = 1; i <= n; i ++){
for (ll j = 1; j <= k; j ++){
string s;
cin >> s;
cnt[i][s]++;
dp[i][s]++;
}
}
// cout << endl;
// for (int i = 1; i <= n; i ++){
// cout << i << " : ";
// for (auto [H, val] : dp[i])
// cout << H << " -- " << val << ", ";
// cout << endl;
// }
// cout << endl;
for (ll i = n - 1; i > 0; i --){
for (auto [S, C] : cnt[i]){
ll sm = 0;
// cout << "HASH = " << H << endl;
string pref = S + '#';
string suff = '#' + S;
map<string, bool> seen;
for (int c = 'a'; c <= 'z'; c ++){
// cout << "can have edge with " << nh << endl;
pref[i] = suff[0] = c;
if (!seen[pref] and dp[i + 1].find(pref) != dp[i + 1].end())
sm = (sm + dp[i + 1][pref]) % mod;
seen[pref] = 1;
if (!seen[suff] and dp[i + 1].find(suff) != dp[i + 1].end())
sm = (sm + dp[i + 1][suff]) % mod;
seen[suff] = 1;
}
dp[i][S] = C * sm % mod;
}
}
// cout << endl;
// for (int i = 1; i <= n; i ++){
// cout << i << " : ";
// for (auto [H, val] : dp[i])
// cout << H << " -- " << val << ", ";
// cout << endl;
// }
// cout << endl;
ll ans = 0;
for (auto [S, val] : dp[1])
ans = (ans + val) % 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... |