Solution (Two Pointers)
For each query we can keep a sliding window where no more than letters in the window are not .
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;int main() {int garland_len;int query_num;string garland;cin >> garland_len >> garland >> query_num;for (int i = 0; i < query_num; i++) {
Java
import java.io.*;import java.util.*;public class Garland {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int garlandLen = Integer.parseInt(br.readLine());String garland = br.readLine();int queryNum = Integer.parseInt(br.readLine());
Alternative Solution (Two Pointers with Prefix Sums)
For each query we can use prefix sums to check the number of occurrences of a given letter and use two pointers to find the maximum Koyomity ending at a given pointer from .
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;const int MAX_N = 1500;int dp[26][MAX_N + 1];int main() {int garland_len;string garland;
Java
import java.io.*;import java.util.*;public class Garland {private final static int MAX_N = 1500;public static void main(String[] args) {Kattio io = new Kattio();int garlandLen = io.nextInt();String garland = io.next();
Alternative Solution (Dynamic Programming)
Warning!
Dynamic programming is a Gold topic.
We are performing up to queries, so it may be optimal to have some way to quickly precompute our answer in constant time.
We can use dynamic programming to cache such answers.
Define to be the best possible score of the first characters, with amount painted of character . Either two things can happen when we add an additional character.
We append the character and paint an additional square.
We append the character, but since it is the same as our previous character we get to "save" a paint.
Since this only gives us the best possible score that ends with length , we can just take the current best and translate it using .
Now, we can query the max painting score for any substring containing the first character. However, this is not required; we only have to query the optimal paintings with size (the full string) with .
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;const int MAX_N = 1500;int dp[MAX_N + 1][MAX_N + 1][26];int garland[MAX_N + 1];int main() {int garland_len;
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!