检查数组值连续出现的频率

时间:2016-02-20 11:19:19

标签: c++ arrays boolean

我有一个字符串" param",说:

param = aaaa;

我有一个数组"字"其中包括:

word = [aaaa,aabb,aacc,aaaa,cccc,bbbb,ccdd,ccbb,ddcc,ccee,aaaa];

我想查看另一个字符串而不是param出现在word上的频率。从上面的例子中,我预计结果是: 2 6

如何连续检查假字符串? 我试图实现这样的代码:

bool check(std::string param, std::vector< std::string > word)
{
    int sum = 0; //sums of consecutive false string

    for (unsigned int limit =0;limit<word.size();limit++)
    {
        if (word[limit]!=param)
        {
            sum = sum + 1;
        }
        else
        {
            sum = sum - 1;
        }

    }

    if (sum>=10)
    {
        return true;
    }
    else
    {
        return false;
    }
}

输入

param : "aaaa"

word : ["aaaa", 
        "bbbb", 
        "bbbb", 
        "bbbb", 
        "bbbb", 
        "bbbb", 
        "bbbb", 
        "bbbb", 
        "bbbb", 
        "bbbb", 
        "bbbb", 
        "aaaa"]

输出==&gt;假

预期输出==&gt;真

3 个答案:

答案 0 :(得分:1)

我终于找到了答案。感谢@πάνταῥεῖ和@Anedar

这是正确的代码:

bool check(std::string param, std::vector< std::string > word)
{
    int sum = 0; //sums of consecutive false string
    std:vector<int> sums; //array of sum value

    for (unsigned int limit =0;limit<word.size();limit++)
    {
        if (word[limit]!=param)
        {
            ++sum;
        }
        else if (sum!=0)
        {
            sums.push_back(sum);
            sum = 0;
        }

    }

    if (sum!=0)
    {
        sums.push_back(sum); //make sure even if word vector does not end with param, the number still get pushed to counter vector
    }

    int temp = 0; //variable to store maximum value of consecutive array
    for (int iter = 0; iter < sums.size(); iter ++)
    {
        if (sums[iter]>temp)
            temp = sums[iter]; //store maximum value of consecutive array
    }

    //now the return bool value
    if (temp>=10)
    {
        return true;
    }
    else
    {
        return false;
    }
}

答案 1 :(得分:0)

好的,首先,你在每个循环中重置了你的总和,所以在你的for循环之后你可以得到的唯一两个可能的结果是1和-1。在连续检查时你真正想要做的是仅在当前条纹完成时重置它,所以你需要的是这样的:

bool check(std::string param, std::vector< std::string > word)
{
    int current=0; //current sum of consecutive false string
    std::vector<int> sums; //finished consecutives are stored here

    for (unsigned int limit =0;limit<word.size();limit++)
    {
        if (word[limit]!=param)
        {
            ++current; 
        }
        else if (current != 0)
        {
            //a consecutive run has just finished
            sums.push_back(current);
            current=0;
        }

    }

    if (current != 0)
        sums.push_back(current);

    //deal with sums here
}

我认为它的作用非常简单:计算当前字符串!= param,如果找到一个参数,则将当前条纹的长度推送到向量。然后你需要以某种方式处理这个向量,或者通过返回它(然后bool是错误的返回类型)或者通过对它进行一些其他检查。

哦和一个小编辑:最后一个如果确保即使你的矢量没有以一个参数结束,数字也会被推送到你的矢量。

答案 2 :(得分:0)

  

预期输出==&gt;真

只需移除else循环内for()部分中的减法以获得预期输出:

bool check(std::string param, std::vector< std::string > word)
{
    int sum = 0; //sums of consecutive false string

    for (unsigned int limit =0;limit<word.size();limit++)
    {
        if (word[limit]!=param)
        {
            sum = sum + 1;
        }
        // else
        // {
        //     sum = sum - 1;
        // }

    }

    if (sum>=10)
    {
        return true;
    }
    else
    {
        return false;
    }
}