#include <iostream>
#include <cstring>
#define MOD 1000000007
using namespace std;
int Q,d,i,j,comb[2005][2005],D[2005][30][30];
int r[2005][30],poz[30][2005],sol[2005][30][30];
char s[2005],x,y;
int main()
{
cin >> (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])%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;
}
cin >> Q;
for (;Q--;)
{
cin >> d >> x >> y;
cout << sol[d][x-'a'][y-'a'] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
1144 KB |
Output is correct |
2 |
Incorrect |
59 ms |
1144 KB |
Output isn't correct |
3 |
Incorrect |
67 ms |
1144 KB |
Output isn't correct |
4 |
Incorrect |
116 ms |
1396 KB |
Output isn't correct |
5 |
Incorrect |
414 ms |
4736 KB |
Output isn't correct |
6 |
Incorrect |
470 ms |
4900 KB |
Output isn't correct |
7 |
Incorrect |
360 ms |
4376 KB |
Output isn't correct |
8 |
Incorrect |
329 ms |
4108 KB |
Output isn't correct |
9 |
Execution timed out |
2043 ms |
38148 KB |
Time limit exceeded |
10 |
Execution timed out |
2021 ms |
38320 KB |
Time limit exceeded |