#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1e9+7;
array<int,3> cs = {23+8,23+14,23+18};
array<int,3> mods = {232323233,232323323,323232323};
array<int,3> hashle(string str){
array<int,3> ret={0,0,0};
array<int,3> co={1,1,1};
for (int i = 0; i < str.length(); i++){
for (int j = 0; j < 3; j++){
ret[j]+=co[j]*str[i];
ret[j]%=mods[j];
co[j]*=cs[j];
co[j]%=mods[j];
}
}
return ret;
}
int32_t main(){
int n,k;
cin>>n>>k;
vector<vector<int>> dp(n,vector<int>(k,0));
vector<vector<string>> _arr(n,vector<string>(k));
for (int i = 0; i < n; i++){
for (int j = 0; j < k; j++){
cin>>_arr[i][j];
}
}
vector<vector<array<int,3>>> sag(n,vector<array<int,3>>(k));
vector<vector<array<int,3>>> sol(n,vector<array<int,3>>(k));
vector<vector<array<int,3>>> hash(n,vector<array<int,3>>(k));
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < k; j++){
hash[i][j]=hashle(_arr[i][j]);
if (i){
sag[i][j]=hashle(_arr[i][j].substr(1,i));
sol[i][j]=hashle(_arr[i][j].substr(0,i));
}
}
}
for (int node = n-1; node >= 0; node--){
for (int flag = 0; flag < k; flag++){
if (node==n-1){
dp[node][flag]=1;
continue;
}
for (int i = 0; i < k; i++){
if (hash[node][flag]==sol[node+1][i] || hash[node][flag]==sag[node+1][i]){
dp[node][flag]+=dp[node+1][i];
dp[node][flag]%=MOD;
}
}
}
}
int ans = 0;
for (int i = 0; i < k; i++){
ans+=dp[0][i];
ans%=MOD;
}
cout<<ans<<endl;
}
Compilation message
trener.cpp: In function 'std::array<long long int, 3> hashle(std::string)':
trener.cpp:10:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for (int i = 0; i < str.length(); i++){
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
1112 KB |
Output is correct |
2 |
Correct |
17 ms |
1112 KB |
Output is correct |
3 |
Correct |
17 ms |
1368 KB |
Output is correct |
4 |
Correct |
17 ms |
1112 KB |
Output is correct |
5 |
Correct |
17 ms |
1116 KB |
Output is correct |
6 |
Correct |
17 ms |
1116 KB |
Output is correct |
7 |
Correct |
17 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
17 ms |
1112 KB |
Output is correct |
6 |
Correct |
17 ms |
1112 KB |
Output is correct |
7 |
Correct |
17 ms |
1368 KB |
Output is correct |
8 |
Correct |
17 ms |
1112 KB |
Output is correct |
9 |
Correct |
17 ms |
1116 KB |
Output is correct |
10 |
Correct |
17 ms |
1116 KB |
Output is correct |
11 |
Correct |
17 ms |
1112 KB |
Output is correct |
12 |
Correct |
889 ms |
12588 KB |
Output is correct |
13 |
Correct |
936 ms |
12744 KB |
Output is correct |
14 |
Correct |
907 ms |
12744 KB |
Output is correct |
15 |
Correct |
889 ms |
12820 KB |
Output is correct |
16 |
Correct |
882 ms |
12624 KB |
Output is correct |
17 |
Correct |
911 ms |
12740 KB |
Output is correct |
18 |
Correct |
927 ms |
12744 KB |
Output is correct |
19 |
Correct |
956 ms |
12740 KB |
Output is correct |
20 |
Correct |
914 ms |
12744 KB |
Output is correct |
21 |
Correct |
925 ms |
12744 KB |
Output is correct |
22 |
Correct |
872 ms |
12624 KB |
Output is correct |