답안 #297614

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
297614 2020-09-11T16:32:02 Z Hehehe Mate (COCI18_mate) C++14
100 / 100
1792 ms 26464 KB
#include<bits/stdc++.h> //:3
#define DIM 2010
#define MOD 1000000007
using namespace std;

//ifstream cin ("date.in");
//ofstream cout ("date.out");
int v[DIM],c[DIM][DIM],f[DIM],dp[DIM][30][30],a[DIM][30];
char l1,l2;
int n,i,j,x,y,q,lg;
int main (){

    string s;
    cin >> s;
    n = s.size();

    for (i=1;i<=n;i++)
        v[i] = s[i - 1] - 'a' + 1;

    c[0][0] = 1;
    for (i=1;i<=n;i++){
        c[i][0] = 1;
        for (j=1;j<=i;j++){
            c[i][j] = c[i-1][j] + c[i-1][j-1];
            if (c[i][j] >= MOD)
                c[i][j] -= MOD;
        }}

    /// a[i][x] - in cate moduri pot selecta s[i],j (am nevoie sa stiu pozitia primului)
    for (i=1;i<=n;i++)
        for (j=i+1;j<=n;j++)
            a[i][v[j]]++;

    /// dp[i][x][y] - cate siruri de lungime i care se termina cu xy pot obtine
    for (lg=2;lg<=n;lg++){
        for (i=lg-1;i<=n;i++){
            x = v[i];
            for (y=1;y<=26;y++){
                dp[lg][x][y] = dp[lg][x][y]+1LL*a[i][y]*c[i-1][lg-2]%MOD;
                if (dp[lg][x][y] >= MOD)
                    dp[lg][x][y] -= MOD;
            }}}


    cin >> q;
    for (;q--;){
        cin >> lg >> l1 >> l2;
        cout << dp[lg][l1-'a'+1][l2-'a'+1] << '\n';
    }


    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 768 KB Output is correct
2 Correct 53 ms 768 KB Output is correct
3 Correct 61 ms 760 KB Output is correct
4 Correct 102 ms 768 KB Output is correct
5 Correct 374 ms 2808 KB Output is correct
6 Correct 419 ms 3048 KB Output is correct
7 Correct 315 ms 2820 KB Output is correct
8 Correct 281 ms 2680 KB Output is correct
9 Correct 1792 ms 26464 KB Output is correct
10 Correct 1767 ms 26460 KB Output is correct