#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int MOD = 1e9+7;
int hsh(string &s,int l,int r){
int p = 31;
int ans= 0;
for(int i=l;i<r;i++){
ans=(1LL*ans*p)%MOD;
ans+=(s[i]-'A');
ans%=MOD;
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,k;cin>>n>>k;
vector<vector<string> > arr(n+1);
for(int i=1;i<=n;i++){
for(int j=0;j<k;j++){
string a;cin>>a;
arr[i].push_back(a);
}
}
int dp[k] = {0};
for(int i=0;i<k;i++) dp[i]=1;
map<int,int> mp;
for(int i=n;i>=1;i--){
if(i!=n){
for(int j=0;j<k;j++){
int left = hsh(arr[i][j],0,i-1);
int right = hsh(arr[i][j],1,i);
int whole = hsh(arr[i][j],0,i);
dp[j] = mp[whole];
if(left==right){
whole = (1LL*whole*31)%MOD;
whole+=(arr[i][j][i-1]-'A');
whole%=MOD;
dp[j]-=mp[whole] ;
}
}
}
mp.clear();
for(int j=0;j<k;j++){
int whole = hsh(arr[i][j],0,i);
int left = hsh(arr[i][j],0,i-1);
int right = hsh(arr[i][j],1,i);
mp[whole]+=dp[j];
mp[left]+=dp[j];
mp[right]+=dp[j];
mp[whole]%=MOD;
mp[left]%=MOD;
mp[right]%=MOD;
}
}
int ans = 0;
for(int i=0;i<k;i++){
ans+=dp[i];
ans%=MOD;
}
cout<<ans<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
708 KB |
Output is correct |
2 |
Correct |
11 ms |
708 KB |
Output is correct |
3 |
Correct |
10 ms |
712 KB |
Output is correct |
4 |
Incorrect |
7 ms |
696 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
11 ms |
708 KB |
Output is correct |
6 |
Correct |
11 ms |
708 KB |
Output is correct |
7 |
Correct |
10 ms |
712 KB |
Output is correct |
8 |
Incorrect |
7 ms |
696 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |