You are on page 1of 4

Khaela Fortunela

05111940000057
PAA – B

1. SPOJ – LNGPALN

LNGPALN - Longest palindrome with no


adjacent duplicates
#dynamic-programming-141 #strings-13

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

print(str, low, high)


fill temp with value 0 to length high
for i=low to high
temp[i-low] = str[i]
if a=0 then copy string temp to lex
a=1
if(a and temp is lexicographically smaller than lex) then
copy string temp to lex

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. }

16. void longestPalindrome(char str[]) {


17. int max = 1, start = 0, begin, end;
18. int len = strlen(str);
19. bool status=0;
20. for (int i = 1; i < len; ++i) {
21. begin = i - 1;
22. end = i + 1;
23. while (begin >= 0 && end < len && str[begin] == str[end]
24. && str[begin]!=str[begin+1] && str[end]!=str[end-1]) {
25. if (end - begin + 1 >= max) {
26. status=1;
27. start = begin;
28. max = end - begin + 1;
29. }
30. --begin;
31. ++end;
32. }
33. if(status) print(str, start, start + max - 1);
34. status=0;
35. }
36.
37. }
38. int T;
39. int main() {
40. scanf("%d", &T);
41. while(T--){
Khaela Fortunela
05111940000057
PAA – B

42. char str[5000];


43. scanf("%s", str);
44. longestPalindrome(str);
45. printf("%s", lex);
46. memset(lex, 0, sizeof(lex)/sizeof(lex[0]));
47.
48. a=0;
49. printf("\n");
50. }
51. return 0;
52. }
53.

SUBMISSION

You might also like