#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef pair<lli,lli> pii;
#define dbg(x) cout<<#x<<" has a value of: "<<x<<"\n";
#define usize(x) (int)(x.size())
#define plist(x) for(int i=0;i<usize(x);i++)cout<<"eleman "<<i<<" = "<<x[i]<<"\n";
#define foreach(x) for(auto item:x)
#define fill(s,x) for(int i=0;i<x;i++)cin>>s[i];
#define in(veriler,a) (veriler.find(a)!=veriler.end())
#define btw(x,y,z) x>=y && x<=z
std::ostream& operator<<(std::ostream& os, pair<int,int> p) {
os << p.first << ", " << p.second;
return os;
}
const lli MOD=1e9+7;
const int MAXN=2005;
lli rmod(lli x){
return ((x%MOD)+MOD)%MOD;
}
lli comb[MAXN][MAXN];
void precalc(){
comb[0][0]=1;
comb[1][0]=comb[1][1]=1;
for (int i = 2; i <MAXN ; i++)
{
comb[i][0]=1;
for (int j = 1; j <= i; j++)
{
comb[i][j]=rmod(comb[i-1][j]+comb[i-1][j-1]);
}
}
}
int main(){
precalc();
string s;
cin>>s;
int n=s.size();
vector<vector<int> > pre(n+1,vector<int>(26));
for (int i = 1; i <= n; i++)
{
for(int j=0;j<26;j++){
pre[i][j]=pre[i-1][j];
}
pre[i][s[i-1]-97]=pre[i-1][s[i-1]-97]+1;
}
vector<vector<vector<int> > > calc(n+1,vector<vector<int> >(n+1,vector<int>(26,0)));
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
for (int k = 0; k < 26; k++)
{
calc[i][j][k]=rmod((pre[n][k]-pre[i][k])*comb[i-1][j]);
}
}
}
vector<vector<vector<int> > > calcpre(26,vector<vector<int> >(n+1,vector<int>(26,0)));
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
for (int k = 0; k < 26; k++)
{
calcpre[s[i-1]-97][j][k]=rmod(calcpre[s[i-1]-97][j][k]+calc[i][j][k]);
}
}
}
int q;
cin>>q;
for (int i = 0; i < q; i++)
{
int x;
cin>>x;
char c1,c2;
cin>>c1>>c2;
cout<<calcpre[c1-97][x-2][c2-97]<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
23672 KB |
Output is correct |
2 |
Correct |
50 ms |
23544 KB |
Output is correct |
3 |
Correct |
54 ms |
23544 KB |
Output is correct |
4 |
Correct |
81 ms |
23800 KB |
Output is correct |
5 |
Correct |
251 ms |
30120 KB |
Output is correct |
6 |
Correct |
282 ms |
29996 KB |
Output is correct |
7 |
Correct |
221 ms |
30248 KB |
Output is correct |
8 |
Correct |
211 ms |
29624 KB |
Output is correct |
9 |
Runtime error |
117 ms |
132096 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
10 |
Runtime error |
119 ms |
132096 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |