Please read the new rule regarding the restriction on the use of AI tools. ×

akhil14shukla's blog

By akhil14shukla, history, 3 years ago, In English

Link to the problem : Cf_739_E I am getting TLE in test case — 5 and have gone through the code many times, but cannot find any bug or optimisation. Any help is appreciated.

ll solve()
{
    string s;
    cin >> s;
    vector<ll> st(26, 0);
    string rm = "";
    string final = "";
    for (ll i = s.size() &mdash; 1; i >= 0; i--)
    {
        if (st[s[i] &mdash; 'a'] == 0)
        {
            st[s[i] &mdash; 'a'] = 1;
            rm = rm + s[i];
        }
        else
        {
            st[s[i] &mdash; 'a']++;
        }
    }
    reverse(rm.begin(), rm.end());
    ll total = 0;
    for (ll i = 0; i < rm.size(); i++)
    {
        if (st[rm[i] &mdash; 'a'] % (i + 1) != 0)
        {
            cout << -1 << endl;
            return 0;
        }
        st[rm[i] &mdash; 'a'] = st[rm[i] &mdash; 'a'] / (i + 1);
        total += st[rm[i] &mdash; 'a'];
    }
    vector<ll> st2(26, 0);
    for (ll i = 0; i < total; i++)
    {
        final = final + s[i];
        st2[s[i] &mdash; 'a']++;
    }
    ll start = total;
    string tmp = final;
    string sb = final;
    for (ll i = 0; i < rm.size(); i++)
    {
        string tmp2 = "";
        for (ll j = 0; j < tmp.size(); j++)
        {
            if (tmp[j] != rm[i])
            {
                tmp2 += tmp[j];
            }
        }
        tmp = tmp2;
        sb += tmp2;
    }
    if (sb != s)
    {
        cout << -1 << endl;
        return 0;
    }
    cout << final << " " << rm << endl;
    return 0;
}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By akhil14shukla, history, 3 years ago, In English

The code below is failing in one test case out of 16. Someone please explain the bug in my code?

    long long n, k;
    cin >> n >> k;
    vector<long long> a(n + 1, 0);
    long long mx = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        mx += a[i];
    }
    if (mx < k)
    {
        cout << 0;
        return 0;
    }
    else if (k == mx)
    {
        cout << 1;
        return 0;
    }
    k = min(k, mx &mdash; k);
    vector<vector<long long>> dp(2, vector<long long>(k + 1, 0));
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= k; j++)
        {
            dp[1][j] += dp[0][j];
            dp[1][j] %= 1000000007;
            if (j + a[i] + 1 <= k)
            {
                dp[1][j + a[i] + 1] -= dp[0][j];
                dp[1][j + a[i] + 1] %= 1000000007;
            }
        }
        dp[0][0] = dp[1][0];

        for (int j = 1; j <= k; j++)
        {
            dp[1][j] += dp[1][j - 1];
            dp[1][j] = dp[1][j] % 1000000007;
            dp[0][j] = dp[1][j];
            dp[1][j - 1] = 0;
        }
        dp[1][k] = 0;
    }
    cout << dp[0][k];

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it