#include<bits/stdc++.h>
using namespace std;
long long const mod = 1e9+7;
unordered_map<string,pair<int,int> > h;
long long dp[56][1505];
int n,k;
int main()
{
int i,j;
string s;
cin>>n>>k;
for(i=1;i<=k;i++)
{
cin>>s;
dp[0][i] = 1;
if(!h[s].first) h[s] = {i,1};
else h[s].second++;
}
for(i=1;i<n;i++)
for(j=1;j<=k;j++)
{
cin>>s;
string l,r;
l = s.substr(0,s.size()-1);
r = s.substr(1,s.size()-1);
//cout<<l<<" "<<r<<endl;
if(h[l].first)
dp[i][j]=(dp[i][j]+dp[i-1][h[l].first]*h[l].second)%mod;
if(h[r].first && l!=r)
dp[i][j]=(dp[i][j]+dp[i-1][h[r].first]*h[r].second)%mod;
if(!h[s].first) h[s] = {j,1};
else h[s].second++;
}
long long ans = 0;
for(i=1;i<=k;i++)
ans=(ans+dp[n-1][i])%mod;
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
1536 KB |
Output is correct |
2 |
Correct |
15 ms |
1536 KB |
Output is correct |
3 |
Correct |
18 ms |
1536 KB |
Output is correct |
4 |
Correct |
12 ms |
640 KB |
Output is correct |
5 |
Correct |
15 ms |
1408 KB |
Output is correct |
6 |
Correct |
19 ms |
1280 KB |
Output is correct |
7 |
Correct |
13 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
16 ms |
1536 KB |
Output is correct |
6 |
Correct |
15 ms |
1536 KB |
Output is correct |
7 |
Correct |
18 ms |
1536 KB |
Output is correct |
8 |
Correct |
12 ms |
640 KB |
Output is correct |
9 |
Correct |
15 ms |
1408 KB |
Output is correct |
10 |
Correct |
19 ms |
1280 KB |
Output is correct |
11 |
Correct |
13 ms |
640 KB |
Output is correct |
12 |
Correct |
197 ms |
16440 KB |
Output is correct |
13 |
Correct |
194 ms |
16312 KB |
Output is correct |
14 |
Correct |
215 ms |
16440 KB |
Output is correct |
15 |
Correct |
203 ms |
16568 KB |
Output is correct |
16 |
Correct |
125 ms |
888 KB |
Output is correct |
17 |
Correct |
167 ms |
10884 KB |
Output is correct |
18 |
Correct |
168 ms |
11144 KB |
Output is correct |
19 |
Correct |
172 ms |
11088 KB |
Output is correct |
20 |
Correct |
167 ms |
11144 KB |
Output is correct |
21 |
Correct |
172 ms |
11016 KB |
Output is correct |
22 |
Correct |
125 ms |
888 KB |
Output is correct |