2016-12-03 34 views
-3

所以我需要在C++程序中确定最长的顺序回文。这里是一个例子v =(3,4,1,5,2,5,1,8,9,6)向我展示5并记住第一个1和最后一个1这种情况的位置。Palindromic vector sequential

#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; 
} 

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

+0

其中乌尔尝试 – Raindrop7

+0

我想在这样的事情..(代码是在问题)。 – Michael

+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; 
    }