简体   繁体   English

程序运行失败的回文子串数

[英]Number of Palindrome Substrings Program Failing to Run

I have a homework assignment that asks us to use dynamic programming to count the number of possible palindrome substrings in a given string.我有一个家庭作业要求我们使用动态编程来计算给定字符串中可能的回文子串的数量。 My program, theoretically, should work, but when I run it, the run fails every time.我的程序理论上应该可以运行,但是当我运行它时,每次都运行失败。 I do not get any compiler errors.我没有收到任何编译器错误。 Here's my code:这是我的代码:

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

/*
 * 
 */
/*bool isPalindrome(string input){
    bool isPal = true;
    int j = (input.length() - 1);
    for(int i = 0; i < (input.length()/2); i++){
        if(input[i] != input[j]){
            isPal = false;
        }
        j--;
    }
    return isPal;
}*/

bool isPalindrome(string input, bool array[5000][5000], int i, int j) {
    int a = i;
    int b = j;

    if(array[i][j])
        return true;

    while(input[a] == input[b]){
        array[a][b] = true;
        a++;
        b--;
        if((b - a) <= 2){
            break;
        }
    }
    return array[i][j];
}

int numPalindrome(string input) {
    bool array[5000][5000];
    for(int k = 0; k < 5000; k++)
        for(int n = 0; n < 5000; n++)
            array[k][n] = false;

    int count = 0;
    for(int i = 0; i < input.length(); i++){
        for(int j = i; j <= input.length(); j++){
            if(isPalindrome(input, array, i, j)){
                count++;
            }
        }
    }
    return count;
}

int main(int argc, char** argv) {
    int count = numPalindrome("xabcba");
    cout << "Count: " << count << endl;
    return 0;
}

Can anyone help find out why my code will not run?谁能帮助找出为什么我的代码无法运行? Thanks.谢谢。

All palindromes in a string can be represented as a graph like this:字符串中的所有回文都可以表示为如下图:

len
 4                    abba
                     /  | \
 3        aba       /   |  \       aca
         / | \     /    |   \     / | \
 2      /  |  \   /    bb    \   /  |  \   aa
       /   |   \ /    /  \    \ /   |   \ /  \
 1    a    b    a    b    b    a    c    a     a

This relation-ship can be used pretty easy to find all palindromes and reducing space-complexity to O(n) .这种关系可以很容易地用于查找所有回文并将空间复杂度降低到O(n) And reducing the runtime-complexity by a great ammount, since we only need to search for all palindromes with length 2 and 3. So unless the input is worstcase ("aaaaaa...aaaaaa" for eg.), we can reduce the number of searched words by a great amount.并且大大减少了运行时的复杂性,因为我们只需要搜索长度为 2 和 3 的所有回文。所以除非输入是最坏的情况(例如“aaaaaa...aaaaaa”),我们可以减少数量大量的搜索词。 The basic idea is to simply store the position of palindromes at an even or uneven position and search for palindromes that are two characters longer.基本思想是简单地将回文的位置存储在偶数或不均匀的位置,然后搜索长两个字符的回文。 Given a list of the positions of palindromes with length n , we can search for palindromes with length n + 2 by simply checking if the character next to the previous palindrome on the left and right side are equal.给定长度为n的回文位置列表,我们可以通过简单地检查左侧和右侧前一个回文旁边的字符是否相等来搜索长度为n + 2的回文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM