The school has recently conducted a survey to investigate the willingness of faculty and staff to participate in group purchases. Employees interested in purchasing are asked to fill out a form.
Since the products are very popular, the school will determine the order of priority for picking up based on the order in which employees fill out the form. If the same employee submits multiple orders, only their last submission will be considered for ordering.
Assuming each employee has a unique representative letter (case-sensitive), if the submission record is "cBBADDDc," it indicates that employee c has two orders, employee B has two orders, employee A has one order, and employee D has three orders. Based on each employee's last submission record, the pickup order would be BADc, meaning employee B has pickup number 1, and employees A, D, and c have pickup numbers 2, 3, and 4, respectively.
Given the form submission records, please write a program to output each employee's pickup number.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The input consists of a string with a length not exceeding 499 characters, representing the form submission records, which will only contain uppercase and lowercase letters. | Please output the pickup order numbers for all employees, sorted by employee code in lexicographical order (A, B, C, …, Z, a, b, …, z), without any whitespace. |
abcde | 12345 |
ABCabc | 123456 |
aABbCc | 235146 |
aaBdccfee | 214365 |
cBBADDDc | 2134 |
BcabdeAREGCGEEFT | 71911121081334256 |
Thought Process
You can first eliminate consecutive identical characters. Then, run a loop and declare a Map. If the current character’s Map value is not zero, use a nested loop (for(auto it: MAP)) to check if it.second is greater than Map[current character], which indicates that this letter’s rank should be decremented by 1. Additionally, declare a count variable initialized to 1. After the nested loop, set Map[current character] to count - 1. If it’s a new character, set Map[current character] to count, then increment count.
When outputting, you can also use for(auto it: MAP) to iterate through the map. Since the map sorts itself, it will output from A to z automatically. You just need to print it.second for each entry to show the ranks of the employees.
Sample Code-ZeroJudge K399: Pickup
#include <iostream>
#include <map>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
string str;
cin >> str;
map<char, int>MAP;
int count = 1;
for (int i = 0; i<str.length(); i++) {
if (i != 0 && str[i] == str[i-1]) continue;
if (MAP[str[i]] != 0) {
for (auto it:MAP) {
if (it.second > MAP[str[i]]) MAP[it.first]--;
}
MAP[str[i]] = count-1;
}
else {
MAP[str[i]] = count;
count++;
}
}
for (auto it:MAP) {
cout << it.second;
}
cout << "\n";
}
//ZeroJudge K399
//Dr. SeanXD