回文矢量序贯

时间:2016-12-03 17:31:07

标签: c++

所以我需要在c ++程序中确定最长的顺序回文。这是一个例子v =(3,4, 1,5,2,5,1 ,8,9,6)给我看5并记住前1和后1的位置我这种情况。

#include<iostream>
using namespace std;
int main(){

    int v[100],n,i,j,max=0,maxi=0,maxj=0,cni,cnj,l;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>v[i];
    cnj=n-1;
    cni=0;
    while(j!=0)
    {
        for(i=0;i<j;i++)
        {
            if(v[i]==v[j])
            {
                l=j-i;
                if(max<j-i)
                {
                    max=j-i;
                    maxi=i;
                    maxj=j;
                }
                j--;
            }
            else
                j=cnj;
        }
        j--;
        cnj=j;
    }
    while(i!=n-1)
    {
        for(j=n;j>i;j--)
        {
            if(v[i]==v[j])
            {
                if(max<j-i)
                {
                    max=j-i;
                    maxi=i;
                    maxj=j;
                }
                i++;
            }
            else
                i=cni;
        }
        i++;
        cni=i;
    }
    cout<<maxi<<"  "<<maxj;

    return 0;
}

当我运行它时,代码块停止工作

1 个答案:

答案 0 :(得分:0)

你可以这样做:

  • 如果只有,如果有两个值相等,则可能是回文开始和

  • 如果我们找到了开始和结束它还不是回文但是我们排除了这个区间之外的所有值并继续检查回文间隔值2,如果两个值不相等那么它不是回文,所以我们打破并再次搜索另外两个相等的值(回文的开始和结束)。

此代码将检查所有回文和副回文(另一回文中的回文)但是如果您只想要一个回文然后在下面取消注释:

    #include <iostream>

    int main()
    {   

        int begIndex = -1, endIndex = -1; // index of begin and end of palindrome if found then they will be set to not -1
        int size; // size of array
        bool IsPalindrome = false; // initialize to flase

        int array[] = {3, 4, 1, 5, 2, 5, 1, 8, 9, 6, 9, 8};
        size = sizeof(array) / sizeof(array[0]); // getting number of elements of array

        for(int i(0); i < size; i++)
        {// for 1
            for(int j(size - 1); j > i; j--) // j begins from the end -1 and stop if it is equal or less than i to ensure not reading out of palindrome interval
            {// for 2
                if(array[i] == array[j]) // comparing if ok set begin and end indexes of palindrome
                {
                    IsPalindrome = true; // if ok then go to the next test
                    begIndex = i; // only save begin and end indexes of palindrome
                    endIndex = j;

                    // checking two by two and any not equal two values will make it non-palindrome and breaks the inner loop and go back again to check
                    for(int k(begIndex), l(endIndex); k < endIndex; k++, l--)
                    {
                        if(array[k] != array[l]) // last check proves it a palindrome so if two elements are not equal here then it's not palindrome break and go back checking again
                        {
                            IsPalindrome = false;
                            begIndex = endIndex = -1; // reset for checking again
                            break;
                        }
                    }
                }
                // note the check is inside second loop to continue checking if next test proves it's not a palindrome
            }// ~for 2

           // now if the last check doesn't prove that it is not a palindrome we print our palindrome:
            if(IsPalindrome)
            {
                for(int m(begIndex); m <= endIndex; m++)
                {
                    std::cout << array[m] << ", ";
                }
                IsPalindrome = false;
                begIndex = endIndex = -1;
               // break; // if you want one palindrome then uncomment break otherwise you'll get all palindomes from biggest to smallest 
                std::cout << std::endl;
            }

        }// ~for 1

        std::cout << std::endl;

        return 0;
    }