I'm trying to do a problem and i have an idea, but it need to push_front a char to string in O(1) (and for each "push_front" I need to compare two new strings too)!
So...can you tell me how to do it please @@
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3773 |
3 | Radewoosh | 3646 |
4 | ecnerwala | 3624 |
5 | jqdai0815 | 3620 |
5 | Benq | 3620 |
7 | orzdevinwang | 3612 |
8 | Geothermal | 3569 |
8 | cnnfls_csy | 3569 |
10 | gyh20 | 3447 |
# | User | Contrib. |
---|---|---|
1 | cry | 161 |
2 | maomao90 | 160 |
3 | awoo | 159 |
4 | atcoder_official | 157 |
5 | -is-this-fft- | 155 |
5 | nor | 155 |
7 | adamant | 153 |
8 | maroonrk | 152 |
8 | Um_nik | 152 |
10 | djm03178 | 146 |
I'm trying to do a problem and i have an idea, but it need to push_front a char to string in O(1) (and for each "push_front" I need to compare two new strings too)!
So...can you tell me how to do it please @@
Name |
---|
Auto comment: topic has been updated by YukatouArimotou (previous revision, new revision, compare).
Auto comment: topic has been updated by YukatouArimotou (previous revision, new revision, compare).
use deque of chars. also there is rope which can insert and delete anywhere in O(logn) but with high constant factor.
can I ask the complexity when comparing 2 deques please?
O(n)
And I forgot, if you use hashing you can compare equality in O(1). I think it's better if you post the problem so people can find a better solution
You can use hashing to insert at the beginning in O(1) too.
Is there any hash template that always AC?
If yes, can I get it please @@
Sorry but the problem is private so I can't show it here.
And...I haven't learned hashing yet @@
Anyway thanks for your help ^^
create array of length 10^6 or whatever and when you gotta do that operation, change the last element first, then the second last, third last, etc etc
but i need to compare 2 strings after each "push_front()" @@
I think you can keep a variable that stores the current position. So like say you have the array [0, 0, 0, 0, 0], your variable stores index 4, and then when you compare, you compare everything from index 5 onwards. You push front an "a", then it becomes [0, 0, 0, 0, "a"], variable becomes 3, and you compare everything from 4 onwards.
Hmmm...It can't help in that problem @@
Anyway thanks for your help ^^
If you're comparing two strings after doing it, the comparison itself will take $$$\mathcal{O}(n)$$$ time anyway, unless the strings are guaranteed to not be too similar for whatever reason. So there is no point of pushing to the front in $$$\mathcal{O}(1)$$$ when it doesn't decrease the time complexity of performing both operations in the worst case.
Ok thank you ^^
Maybe I needa think another idea to solve that problem @@