Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A sequence of positive integers is a palindrome if it reads the same in both directions. The sequences
23, 45, 23 and 23, 45, 56, 23, 23, 56, 45, 23 are examples of palindromes. The sequence 23, 45, 56 is not
a palindrome. The sequence 23, 32 is not a palindrome either. A sequence of length 1 is always a
palindrome.
A given sequence of integers can be broken up into parts such that each of them is a palindrome.
Consider the sequence 34, 45, 34, 56, 34. This can be broken up into 3 palindrome sequences with
34, 45, 34 constituting the first, 56 constituting the second and 34 constituting the third. It can also
be broken in 5 palindrome sequences each containing a single number. Thus, there may be many
different ways to break up a given sequence into palindrome sequences. We want to determine the
smallest number C such that the given sequence can be broken up into C palindrome sequences.
Observe that for any palindrome sequence the value of C is 1. For the sequence 34, 45, 34, 56, 34
the answer is 3. Your aim is to write a program that computes this number for any given sequence.

Input format
• The first line contains N the number of values in the sequence.
• This is followed by a line containing N positive integers separated by space giving the values
of the sequence.

Output format
Output a single integer giving the smalllest number C so that the given sequence can broken up
into C palindrome sequences.

Data
You may assume that all integers in the input are in the range 1 to 10^8
inclusive.
Subtask 1 (100 Marks) 1 ≤ N ≤ 300.

Sample input
5
34 45 34 56 34

Sample output
3
Posted
Updated 4-Nov-15 1:53am
v4
Comments
Philippe Mori 5-Nov-15 12:59pm    
A starting point would be to search for palindrome of length of 2 and 3 and then find on long then can be expanded.

Once you have those sequences, you can separate known sequences (any sequence without overlap except single number sequences) from those that have multiple possibilities.

Then for each of those sequences, find the best choices.

Some optimizations could be made like starting with the longuest sequence...

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

And reading the specification, working out what it involves and designing a solution are important steps you take before you start coding.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
[no name] 4-Nov-15 8:12am    
No i"m sorry, I'm not asking for the whole source code. I just want someone to explain the basic logic for this program, I'll write the code myself, I swear, but I'm just not able to figure it out. And this isn't homework I have this computing test which i enrolled for in 2 weeks, and this was a question in last year's question paper so I wanted to try it out(I got all the other questions but this one). I was trying to figure it out myself, but I need help. I just need help with the logic part of it, my head's hurting thinking of something, just can't think of anything. But sorry for asking such a question, I know it doesn't seem right and if you say so, I'll remove this question. Thanks anyway.
Richard MacCutchan 4-Nov-15 9:04am    
The basic logic is explained in the question.
Mohibur Rashid 4-Nov-15 17:45pm    
This one deserve a 5
Richard MacCutchan 5-Nov-15 3:18am    
:)
The whole point of such a problem is to challenge your analyse skills.
So:
Quote:
I just want someone to explain the basic logic for this program
is like giving you the answer since it is the difficult part of the problem.

When you solve a problem, there is always 2 steps:
- step 1 is discovering the problem logic, it is the difficult part.
- step 2 is translating the logic into the chosen language, almost mechanical.

I recommend you to sharpen your analyse skills:
- Learn one or more analyse methods, I recommend E.W. Djikstra top-Down analyse method, it is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900