Submission #143358

# Submission time Handle Problem Language Result Execution time Memory
143358 2019-08-13T18:56:40 Z nicolaalexandra Mate (COCI18_mate) C++14
80 / 100
2000 ms 26312 KB
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#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 s[DIM],l1,l2;
int n,i,j,x,y,q,lg;
int main (){

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

    c[0][0] = c[1][0] = c[1][1] = 1;
    for (i=2;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])%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)%MOD;
        }}


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



    return 0;
}

Compilation message

mate.cpp: In function 'int main()':
mate.cpp:16:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     cin>>s+1;
          ~^~
# Verdict Execution time Memory Grader output
1 Correct 91 ms 760 KB Output is correct
2 Correct 60 ms 760 KB Output is correct
3 Correct 70 ms 760 KB Output is correct
4 Correct 119 ms 920 KB Output is correct
5 Correct 429 ms 2888 KB Output is correct
6 Correct 483 ms 3064 KB Output is correct
7 Correct 369 ms 2728 KB Output is correct
8 Correct 333 ms 2724 KB Output is correct
9 Execution timed out 2056 ms 26184 KB Time limit exceeded
10 Execution timed out 2065 ms 26312 KB Time limit exceeded