【问题标题】:Find C++ nth occurrence of a substring from a string从字符串中查找 C++ 第 n 次出现的子字符串
【发布时间】:2013-06-10 11:55:20
【问题描述】:

我查看了网站,但没有直接回答以下问题。

在 C++ 中查找字符串中第 n 次出现的子字符串的最有效方法是什么?

此处的示例显示了如何找到第二个匹配项: http://www.cplusplus.com/reference/string/string/find/

但是首先找到第一个匹配项,然后使用该位置搜索下一个匹配项等以找到第 n 个匹配项,似乎效率很低。如果你想要第25场比赛的位置,有没有更快的方法?

编辑:在更大的上下文中,我正在逐行读取文件,对项目的每个响应都有一个分数,有些则丢失了,得到一个NA 字符串。所有项目都用空格分隔。

我希望有排除某些项目的选项,因此仅从项目 35 到 80、90 到 120 和 150-200 进行搜索。 所以我现在做的是这样的:

string blockedLine(string line)
{
  int b_start[] = {35, 90, 150};
  int b_end[] = {80, 120, 200};
  std::vector<int> space_matches = KMP(line, " ");
  string cuttedLine = "";
  for (int i = 0; i < 3; i++)
    {
      cuttedLine.append(line.substr(space_matches[b_start[i]],
                                    space_matches[b_end[i]]));
    }
  return(cuttedLine);
}

KMP 是其中一个 cmets 中提到的函数,它可以获取空间出现的位置,并将它们存储在 space_matches 中。

然后我计算NA 在这个附加字符串中的出现次数。 问题是,如果没有这个附加,在大约 200k 行上读取整行只需要 1 秒。当我使用这种附加方法获取子字符串时,需要 14 秒,这太慢了。

有哪些改进可以加快速度?

【问题讨论】:

  • 我会用谷歌搜索 C++ String Tokenizer,它应该会生成一个列表或一个向量。
  • @ForEveR rfind 不是要找到最后一次出现吗?
  • 有一些巧妙的技巧可用于非常大的数据集,但对于大多数较小的数据集,您最好只在任一端循环。 (显然还取决于您所说的最快是什么意思,以及您是多次搜索还是只搜索一次)
  • 看看这个:en.wikipedia.org/wiki/…
  • 我第二个 @MatsPetersson - 听起来很像 KMP 的工作 (en.wikibooks.org/wiki/Algorithm_Implementation/String_searching/…)

标签: c++ substring


【解决方案1】:
/// Returns the position of the 'Nth' occurrence of 'find' within 'str'.
/// Note that 1==find_Nth( "aaa", 2, "aa" ) [2nd "aa"]
/// - http://stackoverflow.com/questions/17023302/
size_t find_Nth(
    const std::string & str ,   // where to work
    unsigned            N ,     // N'th ocurrence
    const std::string & find    // what to 'find'
) {
    if ( 0==N ) { return std::string::npos; }
    size_t pos,from=0;
    unsigned i=0;
    while ( i<N ) {
        pos=str.find(find,from);
        if ( std::string::npos == pos ) { break; }
        from = pos + 1; // from = pos + find.size();
        ++i;
    }
    return pos;
/**
    It would be more efficient to use a variation of KMP to
    benefit from the failure function.
    - Algorithm inspired by James Kanze.
    - http://stackoverflow.com/questions/20406744/
*/
}

int main() {
    {
        //                       0123456789.123456789.123
        assert(  3 == find_Nth( "My gorila ate the banana", 1 , "gorila") );
        assert( 18 == find_Nth( "My gorila ate the banana", 1 , "banana") );

        //                       0123456789.123456789.123
        assert(  3 == find_Nth( "My banana ate the banana", 1 , "banana") );
        assert( 18 == find_Nth( "My banana ate the banana", 2 , "banana") );

        assert( std::string::npos == find_Nth( "My banana ate the banana", 3 , "banana") );
        assert( std::string::npos == find_Nth( "My banana ate the banana", 3 , "gorila") );
    }
        assert( 1==find_Nth( "aaa", 2, "aa" ) );
        assert( 0==find_Nth( "aaa", 1, "aa" ) );
    {
        std::string str;
        //     01234567
        str = "aaaaaaaa"; assert( 8==str.size() );
        assert( find_Nth( str, 0 , "aa") == std::string::npos );
        assert( find_Nth( str, 1 , "aa") == 0 );
        assert( find_Nth( str, 2 , "aa") == 1 );
        assert( find_Nth( str, 3 , "aa") == 2 );
        assert( find_Nth( str, 4 , "aa") == 3 );
        assert( find_Nth( str, 5 , "aa") == 4 );
        assert( find_Nth( str, 6 , "aa") == 5 );
        assert( find_Nth( str, 7 , "aa") == 6 );
        assert( find_Nth( str, 8 , "aa") == std::string::npos  );
        assert( find_Nth( str, 9 , "aa") == std::string::npos  );
    }
}

【讨论】:

    猜你喜欢
    • 2010-12-25
    • 2018-08-24
    • 2011-02-04
    • 2018-03-22
    • 2021-02-10
    • 2016-05-07
    • 2022-01-18
    • 1970-01-01
    相关资源
    最近更新 更多