我代表一位朋友发帖,因为我觉得这很有趣:
取字符串"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) 我正在寻找一个快速的算法:
我有一个大小为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))
请分享您对此问题的想法,谢谢