提出 #6012966
ソースコード 拡げる
#include <bits/stdc++.h>
#define ll long long int
#define rep(i,a,n) for(ll i=(a); i<(n); i++)
#define chmin(x,y) {x = min(x,y); }
#define chmax(x,y) {x = max(x,y); }
using namespace std;
ll N, M, MOD=1000000007;
vector<ll> s(4000);
vector<ll> t(4000);
// dp[i][j]: i,j番目が部分共通文字列となるときの組み合わせ数
ll dp[4000][4000];
ll memo[4000][4000];
ll sum(ll i, ll j) {
if (memo[i][j] != -1)
return memo[i][j];
ll res;
if (i == 0 && j == 0) {
res= dp[0][0];
} else if (i == 0) {
res = dp[0][j] + sum(0, j-1);
} else if (j == 0) {
res= dp[i][0] + sum(i-1, 0);
} else {
// res = dp[i][j] + sum(i-1, j) + sum(i, j-1) - sum(i-1, j-1);
res = dp[i][j];
(res += sum(i-1, j)) %= MOD;
(res += sum(i, j-1)) %= MOD;
(res += MOD - sum(i-1,j-1)) %= MOD;
}
res %= MOD;
return memo[i][j] = res;
}
signed main() {
cin>>N>>M;
rep(i,0,4000) rep(j,0,4000) memo[i][j]=-1;
rep(i,1,N+1) cin>>s[i];
rep(i,1,M+1) cin>>t[i];
for (ll i=1; i<=N; i++)
for (ll j=1; j<=M; j++) {
if (s[i] == t[j]) {
dp[i][j] = sum(i-1, j-1) + 1;
} else {
dp[i][j] = 0;
}
dp[i][j] %= MOD;
}
ll ans = 1 + sum(N, M);
ans %= MOD;
cout<<ans<<endl;
return 0;
}
提出情報
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
500 / 500 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
s1.txt, s2.txt, s3.txt, s4.txt, s5.txt |
| All |
01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, s1.txt, s2.txt, s3.txt, s4.txt, s5.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 01.txt |
AC |
33 ms |
127232 KiB |
| 02.txt |
AC |
33 ms |
127232 KiB |
| 03.txt |
AC |
33 ms |
127232 KiB |
| 04.txt |
AC |
33 ms |
127232 KiB |
| 05.txt |
AC |
33 ms |
127232 KiB |
| 06.txt |
AC |
313 ms |
188800 KiB |
| 07.txt |
AC |
321 ms |
188800 KiB |
| 08.txt |
AC |
302 ms |
186752 KiB |
| 09.txt |
AC |
307 ms |
188800 KiB |
| 10.txt |
AC |
299 ms |
186752 KiB |
| 11.txt |
AC |
312 ms |
186752 KiB |
| 12.txt |
AC |
310 ms |
186752 KiB |
| 13.txt |
AC |
313 ms |
186752 KiB |
| 14.txt |
AC |
314 ms |
186752 KiB |
| 15.txt |
AC |
305 ms |
186752 KiB |
| 16.txt |
AC |
295 ms |
186752 KiB |
| 17.txt |
AC |
315 ms |
188800 KiB |
| 18.txt |
AC |
296 ms |
186752 KiB |
| 19.txt |
AC |
300 ms |
186752 KiB |
| 20.txt |
AC |
300 ms |
186752 KiB |
| 21.txt |
AC |
307 ms |
188800 KiB |
| 22.txt |
AC |
300 ms |
188800 KiB |
| 23.txt |
AC |
45 ms |
143744 KiB |
| 24.txt |
AC |
283 ms |
186880 KiB |
| 25.txt |
AC |
58 ms |
188800 KiB |
| 26.txt |
AC |
46 ms |
129408 KiB |
| 27.txt |
AC |
321 ms |
188800 KiB |
| 28.txt |
AC |
321 ms |
188800 KiB |
| s1.txt |
AC |
33 ms |
127232 KiB |
| s2.txt |
AC |
33 ms |
127232 KiB |
| s3.txt |
AC |
33 ms |
127232 KiB |
| s4.txt |
AC |
33 ms |
127232 KiB |
| s5.txt |
AC |
33 ms |
127232 KiB |