Problem B: Group Statistics
Given two lines of input: the first line contains numbers, and the second line contains their corresponding group IDs. Count the occurrences of each number in each group and output the statistics.
Problem Analysis
To solve this problem, you can:
- Define two arrays
numbersandgroupsto store the input numbers and their coresponding group IDs, respectively. - Use two additoinal arrays
uniqueNumbersanduniqueGroupsto store distinct numbers and distinct group IDs. Use anexistsarray to track whether a value has already been recorded. - Define a two-dimensional array
count[groupID][number]to store how many times a specific number appears in a specific group. - Final, iterate over the arrays
uniqueNumbersanduniqueGroupsusing nested loops to outputcount[uniqueGroups[i]][uniqueNumbers[j]]as required.
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int numbers[105]; // stores all input numbers
int groups[105]; // stores all group IDs
int uniqueNumbers[105], uniqueGroups[105]; // distinct numbers and groups
bool exists[10005]; // marks if a number or group ID has been seen
int main() {
int testCases, n;
while (scanf("%d", &testCases) != EOF) {
while (testCases--) {
memset(exists, 0, sizeof(exists));
int count[105][10005] = {0}; // count[group][number]
int numLen = 0, groupLen = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
if (!exists[numbers[i]]) {
uniqueNumbers[numLen++] = numbers[i];
exists[numbers[i]] = true;
}
}
memset(exists, 0, sizeof(exists));
for (int i = 0; i < n; i++) {
scanf("%d", &groups[i]);
if (!exists[groups[i]]) {
uniqueGroups[groupLen++] = groups[i];
exists[groups[i]] = true;
}
count[groups[i]][numbers[i]]++;
}
sort(uniqueNumbers, uniqueNumbers + numLen);
sort(uniqueGroups, uniqueGroups + groupLen);
for (int i = 0; i < groupLen; i++) {
printf("%d={", uniqueGroups[i]);
for (int j = 0; j < numLen; j++) {
printf("%d=%d", uniqueNumbers[j], count[uniqueGroups[i]][uniqueNumbers[j]]);
if (j != numLen - 1) printf(",");
else printf("}\n");
}
}
}
}
return 0;
}
Problem D: String Subtraction (20)
Given two strings s1 and s2 (each up to 10^4 characters), remove all characters that appear in s2 from s1 and output the result. Note that both strings may contain spaces.
Problem Clarification
Since spaces may appear, use gets to read the lines.
Solution Approach
Approach 1 (Direct string operations):
- Read
s1ands2usinggetsand assign tostring. - For each character in
s2, find and erase all its occurrences ins1usingfindanderase.
Approach 2 (Using a hash table):
- Read
s1ands2usinggets. - Use a boolean array
hidden(size 200 is sufficient because ASCII values are in [0,127]) initialized tofalse. Sethidden[character] = truefor each character ins2. - Iterate through
s1and output only those characters wherehidden[character]isfalse.
Code 1 (String Operations)
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
char temp[10005];
int main(){
string s1, s2;
int pos;
while (gets(temp) != NULL){
s1 = temp;
gets(temp);
s2 = temp;
for (int i = 0; i < s2.length(); i++){
pos = s1.find(s2[i]);
while (pos != string::npos){
s1.erase(pos, 1);
pos = s1.find(s2[i]);
}
}
cout << s1 << endl;
}
return 0;
}
Code 2 (Hash Table)
#include <cstdio>
#include <cstring>
char s1[10005], s2[10005];
int main() {
while (gets(s1) != NULL) {
bool hidden[200] = {false};
gets(s2);
int len1 = strlen(s1), len2 = strlen(s2);
for (int i = 0; i < len2; i++) {
hidden[s2[i]] = true;
}
for (int i = 0; i < len1; i++) {
if (!hidden[s1[i]]) printf("%c", s1[i]);
}
printf("\n");
}
return 0;
}