# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1109766 | Lakshya108 | Type Printer (IOI08_printer) | C++17 | 446 ms | 2180 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
void min_operations_to_print(vector<string>& words) {
// Sort the words lexicographically to maximize reuse of common prefixes
sort(words.begin(), words.end());
string current_word = ""; // Tracks the current word on the printer
vector<char> operations; // Stores the sequence of operations
for (const string& word : words) {
// Find the longest common prefix length between current_word and word
int common_length = 0;
while (common_length < current_word.size() &&
common_length < word.size() &&
current_word[common_length] == word[common_length]) {
++common_length;
}
// Remove characters beyond the common prefix in the current word
for (int i = current_word.size(); i > common_length; --i) {
operations.push_back('-'); // Remove the last letter
}
// Add the remaining characters of the target word after the common prefix
for (int i = common_length; i < word.size(); ++i) {
operations.push_back(word[i]); // Add each letter to match the target word
}
// Print the word after it's fully formed
operations.push_back('P'); // Print operation
// Update the current word to the last printed word
current_word = word;
}
// Output the number of operations and the operations themselves
cout << operations.size() << endl;
for (char op : operations) {
cout << op << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector<string> words(N);
for (int i = 0; i < N; ++i) {
cin >> words[i];
}
min_operations_to_print(words);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |