Huawei OD Computer Test Algorithm Questions: Alarm Suppression

Question part

topic Huawei OD Computer Test Algorithm Questions: Alarm Suppression
difficulty easy
Question description Alarm suppression refers to the rules for high-priority alarms to suppress low-priority alarms. After a high-priority alarm is generated, a low-priority alarm will no longer be generated. Please provide the actual alarm list based on the original alarm list and alarm suppression relationship.
Note: There will be no loop suppression.
The alarm will not be passed, such as A->B, B->C. In this case, A will not directly suppress C, but the suppressed alarm can still suppress other low-priority alarms.
If there is a suppression relationship between two alarms, the suppressed low-priority alarm will be suppressed regardless of whether it is before or after the high-priority alarm. (Author's note)
Enter description The first line is a number N, indicating the number of alarm suppression relationships, 0<=N <=120.
Next N lines, each line contains two alarm IDs separated by spaces, for example: id1 id2, indicating that id1 suppresses id2. The format of the alarm ID is: uppercase letters + 0 or 1 number.
The last line is the alarm generation list, the list length is [1,100].
Output description List of actual alarms generated.
Additional information Alarm IDs are separated by a single space.
------------------------------------------------------
Example
Example 1
enter 2
A B
B C
A B C D E
output A D E
illustrate A suppresses B, B suppresses C, and the final actual alarm is ADE.
Example 2
enter 4
F G
C B
A G
A0 A
A B C D E
output A C D E
illustrate none


Interpretation and analysis

Question interpretation:

This question requires deleting suppressed alarms from a string and outputting only unsuppressed alarms. The output alarm sequence remains consistent with the source string.
Note: In the alarm list of the topic, if there is a suppression relationship between two alarms, the suppressed low-priority alarm will be suppressed regardless of whether it is before or after the high-priority alarm. It is not stated clearly in the question, but in Example 2, this rule is followed.

There is ambiguity in the description of this question, which needs to be clarified through Example 2. Personally, I think it should be explained clearly in the question stem rather than through examples.

Analysis and ideas:

First declare 2 variables:
1. sourceAlarms, array, input original alarm list.
2. cutOffArray, a two-dimensional array, used to store the alarm suppression relationship. Each element of the array (set to cutOffEle) is a suppression relationship containing two elements. cutOffEle[0] is a high-priority alarm, and cutOffEle[1] is a suppressed low-priority alarm.
3. rmAlarmSet, a collection used to store alarms that need to be deleted in the initial alarm list.

The implementation steps are as follows:
1. Construct cutOffArray. Read the suppression relationship line by line, and each line of data corresponds to a new cutOffEle. Split each row of data into two alarm IDs, put the first alarm ID in cutOffEle[0], the second alarm ID in cutOffEle[1], and then put cutOffEle as an element in the array cutOffArray.
2. Build rmAlarmSet. Traverse cutOffArray, for each element cutOffEle, if cutOffEle[0] exists in sourceAlarms, add cutOffEle[1] to rmAlarmSet.
3. Traverse sourceAlarms. If the alarm ID is not in rmAlarmSet, output it; if it is in rmAlarmSet, skip it.

Note: In step 2, when it is judged that cutOffEle[0] is in sourceAlarms, because sourceAlarms is an array, the time complexity will increase. You can first put all the alarm information in sourceAlarms into a set, such as sourceAlarmSet.

The time complexity of this algorithm is O(n) and the space complexity is O(n).


Code

Java code

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * 告警抑制
 * @since 2023.09.13
 * @version 0.1
 * @author Frank
 *
 */
public class CutOffAlarms {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String input = sc.nextLine();
			int count = Integer.parseInt( input );
			
			String[][] cutOffArray = new String[count][2];
			for( int i = 0; i < count; i ++ )
			{
				input = sc.nextLine();
				String[] curCutOff = input.split( " " );
				cutOffArray[i] = curCutOff;
			}

			input = sc.nextLine();
			String[] sourceAlarms = input.split( " " );
			processCutOffAlarms( cutOffArray, sourceAlarms );
		}
	}

	private static void processCutOffAlarms( String[][] cutOffArray, String sourceAlarms[] )
	{
		Set<String> sourceAlarmSet = new HashSet<String>();
		for( int i = 0; i < sourceAlarms.length; i ++ )
		{
			sourceAlarmSet.add( sourceAlarms[i] );
		}
		
		Set<String> rmAlarmSet = new HashSet<String>();
		for( int i = 0; i < cutOffArray.length; i ++ )
		{
			String[] curAlarmEle = cutOffArray[i];
			if( sourceAlarmSet.contains( curAlarmEle[0] ) ) {
				rmAlarmSet.add( curAlarmEle[1] );
			}
		}
		
		StringBuffer sb = new StringBuffer();
		for( int i = 0; i < sourceAlarms.length; i ++ )
		{
			String eachAlarm = sourceAlarms[i];
			if( rmAlarmSet.contains( eachAlarm ))
			{
				continue;
			}
			sb.append( eachAlarm );
			if( i != sourceAlarms.length - 1 )
			{
				sb.append( " " );
			}
		}
		System.out.println( sb.toString() );
	}
}

JavaScript code

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function() {
    while (line = await readline()) {
        var count = parseInt(line);

        var cutOffArray = new Array();
        for (var i = 0; i < count; i++) {
            line = await readline();
            var curCutOff = line.split(" ");
            cutOffArray[i] = curCutOff;
        }

        line = await readline();
        var sourceAlarms = line.split(" ");
        processUnhappyKids(cutOffArray, sourceAlarms);
    }
}();

function processUnhappyKids(cutOffArray, sourceAlarms) {
    var sourceAlarmSet = new Set();
    for (var i = 0; i < sourceAlarms.length; i++) {
        sourceAlarmSet.add(sourceAlarms[i]);
    }

    var rmAlarmSet = new Set();
    for (var i = 0; i < cutOffArray.length; i++) {
        var curAlarmEle = cutOffArray[i];
        if (sourceAlarmSet.has(curAlarmEle[0])) {
            rmAlarmSet.add(curAlarmEle[1]);
        }
    }

    var ret = "";
    for (var i = 0; i < sourceAlarms.length; i++) {
        var eachAlarm = sourceAlarms[i];
        if (rmAlarmSet.has(eachAlarm)) {
            continue;
        }
        ret += eachAlarm;
        if (i != sourceAlarms.length - 1) {
            ret += " ";
        }
    }

    console.log(ret);
}

(over)

Guess you like

Origin blog.csdn.net/ZiJinShi/article/details/132856184