http://www.spoj.com/problems/VPALIN/

How can Knuth-Morris-Pratt Algorithm (KMP) be used here ?

# | User | Rating |
---|---|---|

1 | tourist | 3557 |

2 | Um_nik | 3395 |

3 | Radewoosh | 3344 |

4 | Benq | 3316 |

5 | LHiC | 3302 |

6 | Petr | 3293 |

7 | wxhtxdy | 3286 |

8 | mnbvmar | 3274 |

9 | yutaka1999 | 3190 |

10 | ainta | 3180 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 191 |

2 | Radewoosh | 177 |

3 | PikMike | 164 |

4 | Vovuh | 163 |

5 | rng_58 | 161 |

6 | majk | 157 |

6 | 300iq | 157 |

8 | antontrygubO_o | 156 |

9 | Um_nik | 149 |

10 | kostka | 148 |

http://www.spoj.com/problems/VPALIN/

How can Knuth-Morris-Pratt Algorithm (KMP) be used here ?

↑

↓

Codeforces (c) Copyright 2010-2019 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Sep/23/2019 09:00:13 (f1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

I think hashing might be a better option. Calculate forward and reverse hashes of each, then for each string, you can count how many hashes of reversed string are there.

Notice that every palindrome (..and each string in general..) can be written by repeating some pattern. For example, "aaa" = "a" * 3, "abab" = "ab" * 2 and "aba" = "aba" * 1. We can observe that two palindromes produce a new palindrome when connected to each other, only if their repeating pattern is the same (so "aa" and "a" works, but "aa" and "aba" doesn't). To find the smallest repeating pattern KMP (or in my case the Z-Algorithm) can be used.

`Can you explain how to find the smallest repeating pattern using Z-algorithm?`

`After finding the Z-array I was doing this:`

`What is wrong in my algorithm?`

`I am not able to find any test case for which this algorithm fails.`

`Please have a look to my algorithm and if possible tell me the mistake or tell your algorithm to solve this query.`

THANKS:).Can you please give a proof for the same fleimgruber.

Can you explain how to handle this case :- Suppose i have strings 'aba' and 'ba' , then repeating subtrings of both of them is 'aba' and 'ba' which means they are not equal but they form a valid palindrome. The question just says that the strings have to be palindrome. But in general I want to ask this.