How can i print all palindrome from a string ? string length is <=10^6. If there is an algorithmic solution or any solution or any Technic please share it. Thanks in advance. :)
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
How can i print all palindrome from a string ? string length is <=10^6. If there is an algorithmic solution or any solution or any Technic please share it. Thanks in advance. :)
Название |
---|
You can try Manacher's algo for finding all palindromes:
Printing all of them will be quite hard as for example if the string is "aaa...aaa" then every substring is a palindrome, also printing the length of each palindrom and the position in the string where it starts is also quite hard as if every substring is a palindrome then there can be, if all the letters are the same, (N*(N+1))/2 palindromes, and that can be about 5*10^11. Counting all the palindromes is a bit easier, you can do it as follows:
As there are palindromes of even length and palindromes of odd length, and we want to count them all (not considering them as different cases) we will change the string in the following way: if the string is "abcda" then we change it to ".a.b.c.d.a.". Then we will find the longest odd length palindrome centered at each letter (odd length palindromes in the original string) and the longest odd length palindrome centered at each dot (even length palindromes in the original string). Once we find the longest palindrome for each letter then if s[i] is a letter and its longest palindrome has length 4t-1 (*1) then we have t palindromes centered at that letter in the original string (it's not hard to prove this) and if s[i] is a dot and its longest palindrome has length 4t+1 (*2) then we have t palindromes centered at that dot in the original string (the dot isn't in the original string but if for example, the palindrome is ".a.a." in the original string it matches "aa").
Now our only task is to find the longest odd length palindrome centered at each character in the new string. We can use the following algorithm that is linear:
Now you just need to count the number of palindromes centered at each character and you solve the problem.
(*1) As the borders of the palindromes will be dots, if the center is a letter the length will be 3 mod 4, for example, ".a." or ".a.b.a.", and so on. (*2) Same as (*1), the palindromes will be ".a.a.", ".a.b.b.a." and so on, so the length will always be 1 mod 4.
And this comment has -9 because...?
You can also try palindromic tree. This concept also helps you to solve some interesting problems related to palindromes. For more details Link