Official Editorial (C++)

Implementation

Note this is a bit different from the implementation outlined in the official editorial. This loops over the start and end of the substrings rather than the length first.

Time Complexity: O(N2)\mathcal{O}(N^2)

C++

Warning: Tight Constraints

Note that when printing out queries, make sure to use "\n" instead of endl or you will get a TLE!

#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0)->sync_with_stdio(false);
string s;
cin >> s;
int n = s.length();
// dp[i][j] = number of palidrome substrings in [i,j]

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!