답안 #145029

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
145029 2019-08-18T12:58:30 Z Mihnea Mate (COCI18_mate) C++14
80 / 100
2000 ms 60816 KB
#include<iostream>
#include<cstring>
#define MOD 1000000007
using namespace std;
long long I;
long long a[2005][2005],b[2005][30],poz[30][2005],sol[2005][30][30],D[2005][30][30];
char s[2005],X,Y;
int main(){
    cin>>(s+1);
    int n=strlen((s+1));
    a[0][0]=1;
    for(int i=1;i<=n;i++){
        a[i][0]=a[i][i]=1;
        for(int j=1;j<i;j++){
            a[i][j]=a[i-1][j-1]+a[i-1][j];
            if(a[i][j]>=MOD)
                a[i][j]-=MOD;
        }
    }
    for(int i=1;i<=n;i++)
        poz[s[i]-'a'][ ++poz[s[i]-'a'][0] ]=i;
    for(int i=n;i>=1;i--)
        for(int j=0;j<26;j++)
            if(s[i]=='a'+j)
                b[i][j]=1+b[i+1][j];
            else
                b[i][j]=b[i+1][j];
    for(int i=n;i>=1;i--)
        for(int j=0;j<26;j++)
            for(int k=0;k<26;k++)
                if(s[i]=='a'+j)
                    D[i][j][k]=b[i+1][k];
                else
                    D[i][j][k]=D[i+1][j][k];
    for(int i=2;i<=n;i++)
        for(int j=0;j<26;j++)
            for(int t=1;t<=poz[j][0];t++){
                int h=poz[j][t];
                if(h>=i-1)
                    for(int k=0;k<26;k++){
                        sol[i][j][k]+=(D[h][j][k]*a[h-1][i-2])%MOD;
                        if(sol[i][j][k]>=MOD)
                            sol[i][j][k]-=MOD;
                    }
            }
    cin>>n;
    while(n!=0){
        cin>>I>>X>>Y;
        cout<<sol[I][X-'a'][Y-'a']<<"\n";
        n--;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 1528 KB Output is correct
2 Correct 59 ms 1400 KB Output is correct
3 Correct 70 ms 1528 KB Output is correct
4 Correct 118 ms 1532 KB Output is correct
5 Correct 425 ms 6044 KB Output is correct
6 Correct 471 ms 6136 KB Output is correct
7 Correct 362 ms 5828 KB Output is correct
8 Correct 329 ms 5624 KB Output is correct
9 Execution timed out 2052 ms 60600 KB Time limit exceeded
10 Execution timed out 2072 ms 60816 KB Time limit exceeded