1

入力文字列があり、それを実行して、特定の単語と一致するかどうかを確認する必要があります。複数の文字列配列がありますが、すべての配列に対して文字列をチェックする効率的な方法がわかりません。

文字列配列:

 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%
...
..
..
4

3 に答える 3

3

次のようなものが必要になる場合があります。

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 */}
    // ...
}
于 2013-09-09T16:47:30.643 に答える
0

配列を調べて、入力文字列内の配列に格納されている単語を検索できます。パフォーマンスを向上させるには、std 関数を使用します。例えば:

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
于 2013-09-09T16:02:47.217 に答える