palindrome - Best algo for finding no. of steps required to convert a sequence to a palindromic sequence -
[my first question of stackoverflow, so, hi!]
i'm not sure of rules around place, have straightforward question follows...
the sequences 23, 45, 23 , 23, 45, 56, 23, 23, 56, 45, 23 examples of palindromes. sequence 23, 45, 56 not palindrome. sequence 23, 32 not palindrome either. sequence of length 1 palindrome. given sequence of integers can broken parts such each of them palindrome. consider sequence 34,45,34,56,34. can broken 3 palindrome sequences 34, 45, 34 constituting first, 56 constituting second , 34 constituting third. can broken in 5 palindrome sequences each containing single number.
we want determine smallest number k such given sequence can broken k palindrome sequences.
#include<iostream> using namespace std; int main() { int n; cin>>n; int num[n]; for(int i=0;i<n;i++) cin>>num[i]; int c[n]; int co=0; for(int i=0;i<n;i++){ if(num[i]!=-1){ for(int j=i+1;j<n;++j){ if(num[i]==num[j]){ num[i]=num[j]=-1; c[co]=j; co++; break; } } } } if(n-co*2==0){ cout<<1; } else{ cout<<n-co*2; } }
Comments
Post a Comment