Professional Documents
Culture Documents
05111940000057
PAA – B
1. SPOJ – LNGPALN
We are given a string . Determine the longest palindromic substring without any adjacent
duplicates.
For example: S="ABBCBBA" ,longest palindromic substring is "ABBCBBBA" but it contains
adjacent duplicates , so the required string is "BCB".
EDIT: If there are multiple such strings then print the lexicographical smallest string.
Input
The first line of input contains a t ,the number of test cases and the following line of each test
case a string S(1<=S<=5000)
Output
Print the required string
EXPLANATION
Firstly, I used regular longest palindrome algorithm that decides on center index then check
to outwards. As adjacent duplicates are not valid, there is no need to think about scenarios for
palindrome with even length. Afterwards, we also need to modify the code to save the string
instead of print them immediately, since when there is multiple possibility of palindrome, the
program needs to choose the smallest string lexicographically. We only need to save the
smallest possible one, and then compares it for every palindrome with longest length. Because
of the we need to modify the condition so that print function is called for every possible
palindrome that is the longest. However, since my code is still not accepted, this might mean
there are some test case or condition I might have missed. This is my current result.
3
DCDBCBACA
DCDABCBAACA
MBBCDCBBM
ACA
ABCBA
BCDCB
Khaela Fortunela
05111940000057
PAA – B
PSEUDOCODE
longestpalindrome(str)
max=1
start = 0
len = length of str
status = 0;
for i to len
begin = i-1
end = i+1
while(begin>=0 and end<len and str[begin] == str [end] and
str[begin] != str [begin+1] and str[end] != str [end-1])
if end-begin+1 >= max then
status = 1
start = begin
max = end-begin+1
--begin
++end
if status then print(str, start, start+max-1)
status = 0
main
Khaela Fortunela
05111940000057
PAA – B
read str
longestpalindrome(str)
print lex
fill lex with value 0
a=0
SOURCE CODE
1. #include <cstdio>
2. #include <string.h>
3. using namespace std;
4. char lex[5000]; int a=0;
5. void print(char str[], int low, int high) {
6. char temp[high-low];
7. memset(temp, 0, high);
8. for(int i = low; i <= high; ++i) {
9. temp[i-low] = str[i];
10. //printf("%c\t", str[i]);
11. }
12. if(!a) strcpy(lex, temp);
13. int a=1;
14. if(a && strcmp(temp, lex)<0) strcpy(lex, temp);
15. }
SUBMISSION