#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
#define int ll
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;
}
bool thesame(string s){
char a = s[0];
for(auto i : s){
if(i!=a) return 0;
}
return 1;
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,k;cin>>n>>k;
int cnt[26];
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 whole = hsh(arr[i][j],0,i);
dp[j] = mp[whole];
if(thesame(arr[i][j])){
dp[j]-=cnt[arr[i][j][0]-'a'];
}
dp[j]%=MOD;
}
}
mp.clear();
for(int i=0;i<26;i++) cnt[i]=0;
for(int j=0;j<k;j++){
if(thesame(arr[i][j])){
cnt[arr[i][j][0]-'a']+=dp[j];
cnt[arr[i][j][0]-'a']%=MOD;
}
int left = hsh(arr[i][j],0,i-1);
int right = hsh(arr[i][j],1,i);
mp[left]+=dp[j];
mp[right]+=dp[j];
mp[left]%=MOD;
mp[right]%=MOD;
}
}
int ans = 0;
for(int i=0;i<k;i++){
ans+=dp[i];
ans%=MOD;
}
if(ans<0) ans+=MOD;
cout<<ans<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
596 KB |
Output is correct |
2 |
Correct |
7 ms |
596 KB |
Output is correct |
3 |
Correct |
7 ms |
700 KB |
Output is correct |
4 |
Correct |
5 ms |
596 KB |
Output is correct |
5 |
Correct |
7 ms |
596 KB |
Output is correct |
6 |
Correct |
7 ms |
596 KB |
Output is correct |
7 |
Correct |
6 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
596 KB |
Output is correct |
6 |
Correct |
7 ms |
596 KB |
Output is correct |
7 |
Correct |
7 ms |
700 KB |
Output is correct |
8 |
Correct |
5 ms |
596 KB |
Output is correct |
9 |
Correct |
7 ms |
596 KB |
Output is correct |
10 |
Correct |
7 ms |
596 KB |
Output is correct |
11 |
Correct |
6 ms |
596 KB |
Output is correct |
12 |
Correct |
102 ms |
5640 KB |
Output is correct |
13 |
Correct |
107 ms |
5628 KB |
Output is correct |
14 |
Correct |
106 ms |
5624 KB |
Output is correct |
15 |
Correct |
111 ms |
5628 KB |
Output is correct |
16 |
Correct |
69 ms |
5440 KB |
Output is correct |
17 |
Correct |
93 ms |
5700 KB |
Output is correct |
18 |
Correct |
99 ms |
5584 KB |
Output is correct |
19 |
Correct |
101 ms |
5588 KB |
Output is correct |
20 |
Correct |
97 ms |
5588 KB |
Output is correct |
21 |
Correct |
96 ms |
5584 KB |
Output is correct |
22 |
Correct |
69 ms |
5440 KB |
Output is correct |