整数数组中最长的回文率

我想在整数数组中找到最大的回文。我尝试制作自己的算法,而不关注在线算法。但这是行不通的。我尝试进行调试,但是无法正常工作。

示例输入:

“ 1367611342142412431113424823782”

输出:113421424124311

void palindrome()
{
    int max = 0;
    int len;
    int start;
    int end;
    int st=0,en=0;
    bool palin = false;
    for(int i=0;i<size;i++)
    {
        for(int j=size-1; j>=0;j--)
        {
            if(array[i] == array[j])
            {
                start = i;
                end = j;
                while(j==i+1 || j+1 == i || j == i )
                {
                    if(array[i] == array[j])
                    {
                        i++; 
                        j--;
                        palin = true;
                    }
                    else
                    {
                        palin = false;
                        break;  
                    }
                }
                i= start;
                j= end;
            }
            if(palin == true)
                {
                    len = end - start;
                    if(len>max)
                    {
                        cout<<" "<<st<<" "<<en<<endl;
                        st=i;
                        en =j;
                        max = len;
                    }
                }
        }
    }
    cout<<endl;
    cout<<st<<" "<<en<<endl;
    ofstream file("output.txt");
    for(int i=st;i<=en;i++)
    {
        file<<array[i];
    }
}

qq125486395 回答:整数数组中最长的回文率

有解决办法

#include <iostream>
#include <string>

struct Result
{   
    int fromIndex,toIndex;
    Result(int fromIndex,int toIndex){
        this->fromIndex = fromIndex;
        this->toIndex = toIndex;
    }

    int length(){
        return toIndex - fromIndex;
    }
};

bool isPalindrome(std::string &s,int left,int right){
    while(left <= right){
        if(s[left] != s[right]){
            return false;
        }
        left ++;
        right --;
    }
    return true;
}

std::string solve(std::string &s){
    int startIndex = 0;
    int toIndex = s.size() - 1;
    Result result(0,0);
    while(true){
        if(isPalindrome(s,startIndex,toIndex)){
            if(result.length() < (toIndex - startIndex)){
                result.fromIndex = startIndex;
                result.toIndex = toIndex;
            }
        }
        toIndex --;
        if(toIndex <= startIndex){
            toIndex = s.size() - 1;
            startIndex++;
        }
        if(startIndex == s.size() - 1){
            break;
        }
    }

    std::string str = "";
    for (int i = result.fromIndex; i <= result.toIndex; ++i)
    {
        str += s[i];
    }

    return str;

}

int main()
{
    std::string s = "1367611342142412431113424823782";
    std::string result = solve(s);
    std::cout << "Longest palindrome is: "<< result;
    return 0;
}

,

问题类似于最长回文子串... https://leetcode.com/problems/longest-palindromic-substring/

这是使用动态编程的这些问题的不错的教程。 https://www.youtube.com/watch?v=0xeBqanD5GQ

,

您需要以更具结构性的方式思考。首先将您的任务分成多个子任务。在这种情况下,有一些子任务:     1.遍历所有可能的组合     2.检查此组合是否是回文。 每个任务都是另一个功能-这样,就更容易思考,阅读代码和调试。 (如果要将其写入文件,这是第三项任务!)

这是“遍历所有可能的组合”的代码。我想您会发现自己如何检查回文式单个阵列。

#include <iostream>
using namespace std;

bool isPalindrome(int* arr,int size);
bool findLargestPalindrome(int* arr,int size);

int main()
{
    int arr[] = { 1,3,6,7,1,4,2,8,2 };
    int arrSize = 31;

    findLargestPalindrome(arr,arrSize);
}

bool findLargestPalindrome(int* arr,int size)
{
    for (int testSize = size; testSize > 0; testSize--)
    {
        int startIndex = 0;
        while (testSize + startIndex <= size)
        {
            int* arrayToTest = &(arr[startIndex]);
            if (isPalindrome(arr,testSize))
            {
                //TODO: you found it - do with it whatever you want
                return true;
            }
            startIndex++;
        }
    }
    return false;
}

bool isPalindrome(int* arr,int size)
{
    //TODO: your code for single palindrome
    return false;
}
,
#include <string.h>
#include <string>
#include <string.h>

#include <iostream>
// using std;
using std::string;

bool isPalindrome(string input)
{
    return (input == string(input.rbegin(),input.rend()));
}

string palindrome(string input)
{
    int size = input.length();

    int longestLength = 0;
    string largedPalindrome;
    string subStr;
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = i + 1; j < size; j++)
        {
            int subLength = j - 1;
            // First,check the substring's length with current result's length
            if(subLength <= longestLength) continue;
            subStr = input.substr(i,j);

            if(isPalindrome(subStr) && longestLength < subLength){
                longestLength = subLength;
                largedPalindrome = subStr;
            }
        }
    }
    return largedPalindrome;
}

int main()
{
    string largedPalindrome = palindrome("1367611342142412431113424823782");
    std::cout<<largedPalindrome<<std::endl;
}

输出:

  

113421424124311

本文链接:https://www.f2er.com/3129590.html

大家都在问