given N queries ( N <= 105 ) of 2 types :
initially , string S is empty.
1 ch : push character ch at the end of the string S
2 : return YES if string S is palindrome else return NO
Input
Output
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
given N queries ( N <= 105 ) of 2 types :
initially , string S is empty.
1 ch : push character ch at the end of the string S
2 : return YES if string S is palindrome else return NO
7
1 a
2
1 b
1 b
2
1 a
2
YES
NO
YES
Название |
---|
Can you give us a link to the problem, so we can be sure that you are not trying to cheat in a contest?
Actually this problem may not be appear in any OJ.
This problem was came in my mind after the previous contest problem C. Bracket Sequence Deletion
And if you feel like I wanna going to cheat something than you can answer the question after a day or whenever you want.
Thanks.
You can simply add characters to string using .push_back function and check if it is palindrome by looping to size/2 and check if s[i] == s[n-i-1]
Sorry I forgot that it will lead to TLE :)
Checking after each query will leads to TLE :(
hashing?
You can simply hash the initial string and its reversed version. After every push_back you can easily recalculate both hashes. It would be something like reversedHash += polynomBase ^ {len(s)}, straightHash = straightHash * polynomBase + getCharHash(newCharacter). To check if the string is palindrome, you only need to know, if reversed hash is equal to usual one.
use Palindromic Tree, if the len of the Last Status equal to n that the string S is palindrome. Palindromic Tree is $$$O(n)$$$
Solve offline
For the text, you can find, if every prefix is palindrome or not by Manacher's algorithm O(N)