Problem Statement:
Bob has a string S and Alice has a string T. Each day, Bob takes his string S and appends it to the final string K. For example, if S is "ab" then K will also be "ab" (on the first day), then on the second day, K becomes "abab". On the third day, K becomes "ababab" and so on.
Your task is to help Alice find and return an integer value, representing the day when the string K contains T as a subsequence for the first time.
Note:
Input Specification:
Output Specification:
Return an integer value representing the day when the string K contains T as a subsequence for the first time.
Problem Statement:
A pet supply store uses specific unit symbols to represent quantities of different pet items, which are defined as follows:
You are given a string S that encodes the list of pet items, and your task is to find and return an integer value representing the total cost of all items in S.
Note: If a unit appears without a number before it, it is assumed to represent a single unit (i.e., quantity = 1). If a number is not followed by a valid unit, treat it as a plain number and add it directly to the total cost.
Input Specification:
Output Specification:
Problem Statement:
You are given an integer array A of size N, where each element represents the current in a wire segment. You can invert (multiply by -1) up to X segments, but two consecutive segments cannot be inverted together due to stability constraints.
Your task is to find and return an integer value representing the maximum possible net current after applying the best inversions. If no valid inversion can be made, return the sum of the original array.
Input Specification:
Output Specification:
Alice wants to know how many times Bob has to repeat his string S (creating string K) until her string T becomes a valid subsequence. Instead of actually building the massive string K in memory (which would cause a Memory Limit Exceeded error), we can simulate the process using two pointers.
C++ Code
#include <iostream>
#include <string>
using namespace std;
int findFirstDay(string s, string t) {
// Quick check to see if it's impossible
for (char c : t) {
if (s.find(c) == string::npos) {
return -1;
}
}
int days = 1;
int j = 0;
int n = s.length();
for (char c : t) {
if (j == n) {
j = 0;
days++;
}
// Find the character in S
while (s[j] != c) {
j++;
if (j == n) {
j = 0;
days++;
}
}
// Match found, move past it for the next character search
j++;
}
return days;
}We need to parse a string that contains a mix of digits and specific unit characters (f, t, c, b). The challenge is handling the implicit rules: a letter without a preceding number means a quantity of 1, and numbers without a valid unit trailing them are treated as flat integer additions.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
long long calculateTotalCost(string s) {
long long total = 0;
long long num = 0;
bool has_num = false;
for (char ch : s) {
if (isdigit(ch)) {
num = num * 10 + (ch - '0');
has_num = true;
} else {
long long qty = has_num ? num : 1;
if (ch == 'f') total += qty * 800;
else if (ch == 't') total += qty * 50;
else if (ch == 'c') total += qty * 200;
else if (ch == 'b') total += qty * 1200;
else {
// Invalid unit: Treat previous digits as a plain number
if (has_num) total += num;
}
// Reset state after processing a unit
num = 0;
has_num = false;
}
}
// Catch any trailing plain numbers at the end of the string
if (has_num) {
total += num;
}
return total;
}Space Complexity: O(1) — Only a few primitive variables are used.
We are allowed to invert up to X values in an array to maximize the sum, but we cannot invert two adjacent items. This is a variation of the "House Robber" problem, but with negative numbers and an exact limit on operations.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long maxNetCurrent(int n, int x, vector<int> a) {
if (n == 0) return 0;
const long long INF = 1e18; // Use large constant for impossible states
vector<long long> prev_not_inv(x + 1, -INF);
vector<long long> prev_inv(x + 1, -INF);
// Base Cases for the 0th element
prev_not_inv[0] = a[0];
if (x >= 1) {
prev_inv[1] = -a[0];
}
for (int i = 1; i < n; ++i) {
vector<long long> curr_not_inv(x + 1, -INF);
vector<long long> curr_inv(x + 1, -INF);
for (int j = 0; j <= x; ++j) {
// Option 1: Do not invert a[i]
if (prev_not_inv[j] != -INF || prev_inv[j] != -INF) {
curr_not_inv[j] = max(prev_not_inv[j], prev_inv[j]) + a[i];
}
// Option 2: Invert a[i] (Requires previous to NOT be inverted)
if (j > 0 && prev_not_inv[j - 1] != -INF) {
curr_inv[j] = prev_not_inv[j - 1] - a[i];
}
}
// Move to the next wire segment
prev_not_inv = curr_not_inv;
prev_inv = curr_inv;
}
long long max_current = -INF;
for (int j = 0; j <= x; ++j) {
max_current = max({max_current, prev_not_inv[j], prev_inv[j]});
}
return max_current;
}