# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
143654 | 2019-08-14T19:52:32 Z | vladciuperceanu | Mate (COCI18_mate) | C++14 | 546 ms | 56824 KB |
#include <cstdio> #include <cstring> #define MOD 1000000007 using namespace std; long long Q,d,i,j,comb[2005][2005],D[2005][30][30]; long long r[2005][30],poz[30][2005],sol[2005][30][30]; char s[2005],x,y; int main() { scanf("%s", (s+1)); int n = strlen((s+1)); comb[0][0] = 1; for (i=1; i<=n; i++) { comb[i][0] = comb[i][i] = 1; for (j=1; j<i; j++) { comb[i][j] = comb[i-1][j-1]+comb[i-1][j]; if (comb[i][j] >= MOD) comb[i][j] -= MOD; } } for (i=1; i<=n; i++) poz[s[i]-'a'][++poz[s[i]-'a'][0]] = i; for (i=n; i>=1; i--) for (j=0; j<26; j++) if (s[i] == 'a'+j) r[i][j] = 1+r[i+1][j]; else r[i][j] = r[i+1][j]; for (i=n; i>=1; i--) for (j=0; j<26; j++) for (int k=0; k<26; k++) if (s[i] == 'a'+j) D[i][j][k] = r[i+1][k]; else D[i][j][k] = D[i+1][j][k]; for (int l=2; l<=n; l++) for (j=0; j<26; j++) for (int t=1; t<=poz[j][0]; t++) { i = poz[j][t]; if (i >= l-1) for (int k=0; k<26; k++) { sol[l][j][k] += (D[i][j][k]*comb[i-1][l-2])%MOD; if (sol[l][j][k] >= MOD) sol[l][j][k] -= MOD; } } scanf("%lld", &Q); for (;Q--;) { scanf("%lld %c %c", &d, &x, &y); printf("%lld\n", sol[d][x-'a'][y-'a']); } return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 1400 KB | Output is correct |
2 | Correct | 8 ms | 1272 KB | Output is correct |
3 | Correct | 8 ms | 1276 KB | Output is correct |
4 | Correct | 12 ms | 1400 KB | Output is correct |
5 | Correct | 47 ms | 5112 KB | Output is correct |
6 | Correct | 52 ms | 5240 KB | Output is correct |
7 | Correct | 41 ms | 4984 KB | Output is correct |
8 | Correct | 37 ms | 4856 KB | Output is correct |
9 | Correct | 546 ms | 56824 KB | Output is correct |
10 | Correct | 541 ms | 56696 KB | Output is correct |