相关疑难解决方法(0)

通过从字符串中选择字符可以形成多少个回文?

我代表一位朋友发帖,因为我觉得这很有趣:

取字符串"abb".通过遗漏少于字符串长度的任意数量的字母,我们最终得到7个字符串.

abb ab ab bb abb

其中4个是回文.

同样对于字符串

"hihellolookhavealookatthispalindromexxqwertyuiopasdfghjklzxcvbnmmnbvcxzlkjhgfdsapoiuytrewqxxsoundsfamiliardoesit"

(长度为112弦)2 ^ 112 - 可以形成1个弦.

其中有多少是回文?

下面是他的实现(在C++中,C也很好).用很长的词来说它很慢; 他想知道什么是最快的算法(我也很好奇:D).

#include <iostream>
#include <cstring>

using namespace std;



void find_palindrome(const char* str, const char* max, long& count)
{
    for(const char* begin = str; begin < max; begin++) {
        count++;
        const char* end = strchr(begin + 1, *begin);
        while(end != NULL) {
            count++;
            find_palindrome(begin + 1, end, count);
            end = strchr(end + 1, *begin);
        }
    }
}


int main(int argc, char *argv[])
{
    const …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm performance

23
推荐指数
2
解决办法
2万
查看次数

给定一组数字,找出其中3个加起来为0

给定一组数字,找出其中3个加起来为0.

在N ^ 2做,怎么会这样做?

algorithm

12
推荐指数
3
解决办法
2万
查看次数

在数组中求和的快速算法

我正在寻找一个快速的算法:

我有一个大小为n的int数组,目标是找到数组中的所有模式
x1, x2, x3 are different elements in the array, such that x1+x2 = x3

例如,我知道有一个大小为3的int数组,[1, 2, 3]那么只有一种可能性:1 + 2 = 3(考虑1 + 2 = 2 + 1)

我正在考虑实现Pairs和Hashmaps以使算法快速.(我现在得到的最快的还是O(n^2))

请分享您对此问题的想法,谢谢

arrays algorithm

5
推荐指数
1
解决办法
2898
查看次数

标签 统计

algorithm ×3

arrays ×1

c++ ×1

performance ×1