String 使用递归方法从字符串中删除一个字符…@Oli Charlesworth:对不起,我没有任何线索:(这与C++或任何特定的编程语言无关。你反问题找到字符串中回文的总数)?字符串中的任何改变都将保持它是回文还是不存在,没有第三个选项,所以找到一个将给你另一个。
String 使用递归方法从字符串中删除一个字符…@Oli Charlesworth:对不起,我没有任何线索:(这与C++或任何特定的编程语言无关。你反问题找到字符串中回文的总数)?字符串中的任何改变都将保持它是回文还是不存在,没有第三个选项,所以找到一个将给你另一个。,string,algorithm,math,String,Algorithm,Math,使用递归方法从字符串中删除一个字符…@Oli Charlesworth:对不起,我没有任何线索:(这与C++或任何特定的编程语言无关。你反问题找到字符串中回文的总数)?字符串中的任何改变都将保持它是回文还是不存在,没有第三个选项,所以找到一个将给你另一个。 numWays(str): return 0 if str is empty return memo[str] if it exists memo[str] = numWays(str
使用递归方法从字符串中删除一个字符…@Oli Charlesworth:对不起,我没有任何线索:(这与C++或任何特定的编程语言无关。你反问题找到字符串中回文的总数)?字符串中的任何改变都将保持它是回文还是不存在,没有第三个选项,所以找到一个将给你另一个。
numWays(str): return 0 if str is empty
return memo[str] if it exists
memo[str] = numWays(str - firstChar) +
numWays(str - secondChar) +
... +
1 if str is not a palindrome
return memo[str]
#include <stdio.h>
#include <string.h>
char string[] = "AbbA";
int is_palindrome (char *str, size_t len, size_t mask);
int main(void)
{
size_t len,mask, count;
len = strlen(string);
count =0;
for (mask = 1; mask < (1ul <<len) -1; mask++) {
if ( is_palindrome (string, len, mask)) continue;
count++;
}
fprintf(stderr, "Len:=%u, Count=%u \n"
, (unsigned) len , (unsigned) count );
return 0;
}
int is_palindrome (char *str, size_t len, size_t mask)
{
size_t l,r,pop;
for (pop=l=0, r = len -1; l < r; ) {
if ( mask & (1u <<l)) { l++; continue; }
if ( mask & (1u <<r)) { r--; continue; }
if ( str[l] == str[r] ) return 1;
l++,r--; pop++;
}
return (pop <1) ? 1: 0;
}
import Data.List
listNonPalindromes string =
filter (isNotPalindrome) (subsequences string)
where isNotPalindrome str
| fst substr == snd substr = False
| otherwise = True
where substr = let a = splitAt (div (length str) 2) str
in (reverse (fst a), if even (length str)
then snd a
else drop 1 (snd a))
howManyNonPalindromes string = length $ listNonPalindromes string
# Count the palindromic subsequences of s
def pcount(s):
length = len(s)
p0 = [0] * (length + 1)
p1 = [1] * length
for l in range(1, length):
for i in range(length - l):
p0[i] = p1[i]
if s[i] == s[i + l]:
p1[i] += p1[i+1] + 1
else:
p1[i] += p1[i+1] - p0[i+1]
# The "+ 1" is to account for the empty sequence, which is a palindrome.
return p1[0] + 1
# Count the non-palindromic subsequences of s
def npcount(s):
return 2**len(s) - pcount(s)