1

I have a input string and need to run through it and see if it matches certain words. I have multiple string arrays but not sure whats an efficient way to check the string agianst all the arrays.

String Arrays:

 string checkPlayType(string printDescription)
{
    const string DeepPassRight[3] = {"deep" , "pass" , "right"};
    const string DeepPassLeft[3] = {"deep" , "pass" , "left"};
    const string DeepPassMiddle[3] = {"deep" , "pass" , "middle"};

    const string ShortPassRight[3] = {"short" , "pass" , "right"};
    const string ShortPassLeft[3] = {"short" , "pass" , "left"};
    const string ShortPassMiddle[3] = {"short" , "pass" , "middle"};

    //Must contain right but not pass
    const string RunRight = "right";
    //Must contain right but not pass
    const string RunLeft = "left";
    //Must contain middle but not pass      
    const string RunMiddle = "middle";

    const string FieldGoalAttempt[2] = {"field" , "goal" };
    const string Punt = "punt";

}

Sample Input: (13:55) (Shotgun) P.Manning pass incomplete short right to M.Harrison.

Assuming this is our only input...
Sample Output: 
Deep Pass Right: 0%
Deep Pass Left: 0%
Deep Pass Middle: 0%
Short Pass Right: 100%
Shor Pass Left:0%
...
..
..
7
  • 1
    Why do you have all of these separate string arrays instead of a single dictionary to loop through? Commented Sep 9, 2013 at 15:57
  • I have to identify what type of play it is and then output the percentage that play happens Commented Sep 9, 2013 at 15:58
  • 2
    Can you give example input and desired output? Commented Sep 9, 2013 at 15:58
  • 1
    Take a look at Boyer Moore or KMP algorithms if you want to write them yourself. en.wikipedia.org/wiki/… en.wikipedia.org/wiki/… Commented Sep 9, 2013 at 16:01
  • Updated with sample input/output Commented Sep 9, 2013 at 16:04

3 Answers 3

3

you may want something similar to:

void checkPlayType(const std::vector<std::string>& input)
{
    std::set<std::string> s;

    for (const auto& word : input) {
        s.insert(word);
    }
    const bool deep_present = s.count("deep");
    const bool pass_present = s.count("pass");
    const bool right_present = s.count("right");
    const bool left_present = s.count("left");
    // ...

    if (deep_present && pass_present && right_present) { /* increase DeepPassRight counter */}
    if (deep_present && pass_present && left_present) { /* increase DeepPassLeft counter */}
    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

@moswald: I agree, your edit is more comprehensible (use short circuit && instead of bitwise &). The version with & is correct too and use less branch.
0

Try regular expressions:

if found "pass" then
    if regexp "(deep|short).*(left|right|middle)"
        Hooray!
    else if regexp "(left|right|middle).*(deep|short)"
        Hooray!
    else
        Aye, Caramba!
else
    Aye, Caramba!

Comments

0

You can go over your arrays and search for the words are stored in the array within the input string. Use std functions for better performance. For example:

const string DeepPassRight[3] = {"deep" , "pass" , "right"};
    int i = 0;
    for(;i<3;i++)
    {
        string s = " ";
        s.append(DeepPassRight[i]);
        s.append(" ");
        std::size_t found = printDescription.find(s);
        if (found ==std::string::npos)
           break;

    }
    if(i == 3)
        // printDescription contains all DeepPassRight's members!
if(i== 2)
// just two words were found

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.