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;
//#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |