Hashing: Group Statistics and String Subtraction

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:

  1. Define two arrays numbers and groups to store the input numbers and their coresponding group IDs, respectively.
  2. Use two additoinal arrays uniqueNumbers and uniqueGroups to store distinct numbers and distinct group IDs. Use an exists array to track whether a value has already been recorded.
  3. Define a two-dimensional array count[groupID][number] to store how many times a specific number appears in a specific group.
  4. Final, iterate over the arrays uniqueNumbers and uniqueGroups using nested loops to output count[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):

  1. Read s1 and s2 using gets and assign to string.
  2. For each character in s2, find and erase all its occurrences in s1 using find and erase.

Approach 2 (Using a hash table):

  1. Read s1 and s2 using gets.
  2. Use a boolean array hidden (size 200 is sufficient because ASCII values are in [0,127]) initialized to false. Set hidden[character] = true for each character in s2.
  3. Iterate through s1 and output only those characters where hidden[character] is false.

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;
}

Tags: Hashing string Data Structures algorithms

Posted on Thu, 25 Jun 2026 16:59:50 +0000 by ReDucTor