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

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

Before contest

Codeforces Round #647 (Div. 1) - Thanks, Algo Muse!

25:11:14

Register now »

Codeforces Round #647 (Div. 1) - Thanks, Algo Muse!

25:11:14

Register now »

*has extra registration

Before contest

Codeforces Round #647 (Div. 2) - Thanks, Algo Muse!

25:11:14

Register now »

Codeforces Round #647 (Div. 2) - Thanks, Algo Muse!

25:11:14

Register now »

*has extra registration

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

1 | MiFaFaOvO | 3681 |

2 | Um_nik | 3544 |

3 | maroonrk | 3431 |

4 | tourist | 3409 |

5 | apiadu | 3397 |

6 | 300iq | 3317 |

7 | ecnerwala | 3260 |

7 | Benq | 3260 |

9 | LHiC | 3229 |

10 | TLE | 3223 |

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

1 | Errichto | 193 |

2 | antontrygubO_o | 191 |

3 | vovuh | 178 |

4 | pikmike | 177 |

5 | tourist | 166 |

6 | Um_nik | 165 |

7 | McDic | 164 |

8 | ko_osaga | 163 |

9 | Radewoosh | 161 |

10 | Geothermal | 158 |

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

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

↑

↓

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/03/2020 16:23:47 (g1).

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.