HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Matching query patterns with previously stored strings

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
storedwithquerypreviouslypatternsstringsmatching

Problem

Program description

The goal of this program is to get a number, which is the number of lines of inputs (lets call that number N) that the program will receive. Then the following N lines will consist of two parts, first a char which is either 'T' or 'M'. The next part will be a string that is a little different for 'T' and 'M'. If the char is 'T' then the string will consist of the following chars: '-', '+' and '0'. If the first char in the input line is 'M' then the following string can consist of the following chars: '-', '+', '0' and '?'.

When the line starts with a 'T', then the string will be stored. I store it in an array and the entries will be numbered from 1 to the number of entries.

When the line starts with an 'M', then the string will be compared to the list of strings that are stored to find out which of them that it matches best with.

The program will then output after each input line starting with 'M' the number associated with the stored string that the tested string matches best with. If there are more that each share the best match then it will output the one with the highest number associated with it.

The program check if the strings match from right to left.

If a string after an 'M' contains the char '?' it can be any char ('-', '+' and '0').

The way I build the program was by making an array for the stored strings, then making a function that can get a "match score" when trying to see which of the stored strings match best with the string being tested. If the match score is equal to the length of the tested string then it is the best possible match.

The program does not care about matching more then the chars in the tested string, so if we tried tried matching "0++--0" to "++0++--0" it would give os a match score of 6 which means it would be the best possible match.
Program execution

```
#include

//For strcpy:
#include
#include
using namespace std;

//Gets match score for checking which match is the best of all. The highest match v

Solution

One thing I noticed with your existing code. This:

char matchCheckArr[50], matchTargetArr[50];

//These two lines copy the strings for check and target into their respective char arrays.
strcpy(matchCheckArr, matchCheck.c_str());
strcpy(matchTargetArr, matchTarget.c_str());


is redundant and a waste of resources, since you're only checking each char and matchCheck and matchTarget can each be accessed by indexing.

On a side note, goto is one of the worst programming structures ever created. There is nothing you can do with goto that can't be achieved with more reasonable structures. In your case a simple break statement will break out of the loop and execute the first line after the loop.

Code Snippets

char matchCheckArr[50], matchTargetArr[50];

//These two lines copy the strings for check and target into their respective char arrays.
strcpy(matchCheckArr, matchCheck.c_str());
strcpy(matchTargetArr, matchTarget.c_str());

Context

StackExchange Code Review Q#156429, answer score: 3

Revisions (0)

No revisions yet.