Блог пользователя isaf27

Автор isaf27, история, 6 лет назад, По-английски

The problems A, C, D, G are authored and prepared by isaf27.

The problems B, E, F are authored and prepared by 300iq.

Tutorial is loading...

Jury solution: link

Tutorial is loading...

Jury solution: link

Tutorial is loading...

Jury solution: link

Tutorial is loading...

Jury solution: link

Tutorial is loading...

Jury solution: link

Tutorial is loading...

Jury solution: link

Tutorial is loading...

Jury solution: link

Let's discuss your ideas and solutions in the comments. Thanks for your participation!

Разбор задач Codeforces Global Round 7
  • Проголосовать: нравится
  • +315
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +21 Проголосовать: не нравится

Thanks for the fast editorial ! :)

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Thanks for Editorial giving so fast

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For the jury solution of D, on line 41, shouldn't the substring be from L to size — L, not from L to size — 2 * L?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +38 Проголосовать: не нравится

    In the C++ substring function, the arguments are (start index, length), not (start index, end index). Since we trim a prefix and a suffix of length L from the string, the length should in fact be size — 2 * L.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +26 Проголосовать: не нравится

can anyone give me detailed explanation of E?
Thanks in advance!!

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +83 Проголосовать: не нравится

    I get the conclusion in the tutorial by Hall Theorem during the contest.

    This is my understanding: To check whether answer is $$$x$$$, we consider the largest $$$n-x$$$ number, and link edges to the bombs on it right, so we need to check whether there is a perfect match. According to Hall Theorem, every subset of these number $$$A$$$, and it links bombs set $$$B$$$, $$$|A| \le |B|$$$. Actually, we do not need to check all the subset, we only need to check all the suffixs, and this is the conclusion in the tutorial. And you can see, if all the suffixs satisfy this condition, all the subset also satisfy it.

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +1 Проголосовать: не нравится

      Can you please give some detail into how actually segment trees can be used to check the condition? I mean what are we actually maintaining in the nodes and how are we using it?

      • »
        »
        »
        »
        6 лет назад, скрыть # ^ |
        Rev. 2  
        Проголосовать: нравится +6 Проголосовать: не нравится

        The check condition is:

        There is at least $$$1$$$ bomb after the rightmost value which is $$$\ge n - x$$$.

        There are at least $$$2$$$ bombs after the next rightmost value which is $$$\ge n - x$$$.

        ...

        There are at least $$$x$$$ bombs after the $$$x$$$-th rightmost value which is $$$\ge n - x$$$.

        So each of the key position $$$p$$$ whose value is $$$\ge n-x$$$ should satisfy that the number of suffix key position is larger than the number of suffix bombs. We minus these two suffix sum, and in each node of segment tree, we actually maintain the number of suffix bombs minus the number of suffix key position.

        When we add a bomb at $$$q$$$, all the value in $$$[1, q]$$$ should $$$+1$$$. When we try to add a key position $$$p$$$, all the value in $$$[1,p]$$$ should $$$-1$$$. And if some position's value is negative, check failed. So we only need to maintain global minimum.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone please give a detailed explanation of D? Thanks.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone explain the given solution for 1326D2 — Prefix-Suffix Palindrome (Hard version). For example, if the input string is "xkcddckyyx", the answer should be "xkcddckx" a="xkcddck" and b="x", can somebody explain how you will reach the value of 'k' ( k is given in the solution above) here?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In the third question, the explanation for the third test case seems to be wrong which was quite confusing.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

May be its too basic, but I couldn't understand the mathematics behind solution of problem A. Can anyone explain how this works? I know this solution will work but unable to know how to come up with this solution

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone pls explain how the second part of problem C is done?? i mean why ai+1-ai works here in finding number of segments?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +14 Проголосовать: не нравится

    Let's denote the places of the largest elements by stars, and the other elements by dashes. For example, it might look like:

    --*--**--*-
    

    Then our goal is to count the number of ways to add dividers that split up all the stars. For example:

    --*-|-*|*--|*-
    

    The i-th divider must be between $$$a_{i}$$$ and $$$a_{i+1},$$$ so it has $$$a_{i+1}-a_i$$$ places it can go. The choices for all dividers are independent, so we multiply all these numbers together.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +8 Проголосовать: не нравится

    Lets first solve the following problem, you have $$$n$$$ different eggs in a row, you want to take a prefix from eggs(probably empty or all eggs), how many ways you can choose the prefix?, lets say that we want to choose a border and then our prefix is the eggs in the left of the border(border is just a stick between two eggs or before or after all eggs), you can choose the border in $$$n+1$$$ ways.

    Now go back to the problem $$$C$$$, its easy to see that every segment must have exactly one number bigger than to $$$n-k$$$, so lets change it a little bit, we want to find exactly $$$k-1$$$ borders, such that first border is between $$$a_1$$$ and $$$a_2$$$, second border is between $$$a_2$$$ and $$$a_3$$$ and so far, what you think the answer will be?($$$a_i$$$ is $$$ith$$$ number in the array which is bigger than $$$n-k$$$), choosing border are independent, there is exactly $$$a_{i+1} - a_i$$$ ways to choose $$$ith$$$ border.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

I don't understand why, but problems B, C and D1 were much more intuitive to me than A was.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +15 Проголосовать: не нравится

I have slightly different approach for problem E which does not use observation in the editorial. First how to check that the answer is at least $$$x$$$? Let $$$a_i = 1$$$ if $$$p_i \ge x$$$ and $$$0$$$ otherwise. To check it in linear time go from left to right maintainng $$$cnt_1$$$ — current number of ones. If $$$a_i=1$$$ increment $$$cnt_1$$$ by one. If there is a bomb in the $$$i$$$-th cell do $$$cnt_1 = max(cnt_1 - 1, 0)$$$. If $$$cnt_1 \gt 0$$$ at the end then the answer is at least $$$x$$$. To solve original problem maintain current answer which equals to $$$n$$$ at the beginning, and you have two type of events : assign $$$a_i = 1$$$ and put a bomb in some cell. You can do it with segment tree storing for each node $$$cnt_1$$$ — the number of ones left at the end if we will go through this segment and $$$cnt_0$$$ — the number of times there was a bomb but there was not a one($$$cnt_1 = 0$$$). With this data we can calculate desired $$$cnt_1$$$ at the end. And as answer never increases at the beginning of each iteration decrease current answer until $$$cnt_1 \gt 0$$$ at the end and at the end of each iteration do an update of the bomb.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +11 Проголосовать: не нравится

    Your solution is very intuitive. Thanks a lot.

    I noticed that bombs could be modeled as ')' and numbers greater than current ans i.e 1s as '('. Now this question is similar to regular bracket sequence.

    I was getting WA earlier as I was using -1 for bombs earlier. But this bomb cancels even numbers on the right. Hence we needed to maintain two values in the segment tree.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    ABalobanov, can you provide a link to your submission? I'm having trouble understanding how the segment tree is maintained. Specifically, what information is stored at each index?

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Here's my submission 73741605. To calculate amount of ones left at the end we should also store another value — the number of times there was a bomb but there was not a one. We should do it because we do not decrease number of ones if there are no of them. So we store these two values in every node of the segment tree. And with this information we can calculate these values in the node using values in it's left and right child. You can see how to do it in my code. Ask if you have questions.

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +73 Проголосовать: не нравится

There is a beautiful solution to problem E using partial retroactive priority queues. In a partial retroactive priority queue, it is allowed to perform five types of operations:

  • Insert / Delete a Push(x) operation at time t;
  • Insert / Delete a Pop() operation at time t;
  • Get the smallest element inside the priority queue at present time.

Demaine describes in the following article how to implement a partial retroactive priority queue in $$$O(\lg n)$$$ time per operation.

So, you can Push all values $$$p_i$$$ inside the data structure at time $$$i$$$ and, after each bomb, insert a Pop operation at time $$$q_i$$$.

If you want more details, you can see my code here

Ps. Thanks to Kallaseldor for the idea to solve the problem using retroactive priority queue

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Wy this submission My submission to problem D2 gives TLE if the complexity is O (n)

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    Instead of doing this

    p = p + s[i];
    pp = s[j] + pp;
    

    Try doing this :

    p += s[i];
    pp += s[j];
    reverse(pp.begin(),pp.end());
    

    Cuz the above operations takes O(|p|) each time you add a character, but the this method appends the characters into string taking O(1) each time you add a character to string.

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      I thought the operation y = s [j] + y was O (1). Thank you very much I still did not find the error and it cost me 13 TLEs in the competition and in the end I could not accept it.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Could someone point out the idea behind solving F1 (that won't work for the harder version)? Thanks.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    For example this 73757219 naive recursion with memoization. calc(mask, last) calculates the answer for the rest of the men (given by mask) with last being currently the last man in the sequence. Complexity seems to be O(n*2^(2n)) with a good constant.

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 4  
    Проголосовать: нравится +11 Проголосовать: не нравится

    I ran through all of the partitions of the people by two (almost) equal groups (there are C_14^7 of them, it's around 3500).

    For the first half of every partition I pre-counted d[fin][res] – the number of ways to go through this half stopping in fin and getting a result mask equal to res.

    For the second half of every partition I pre-counted d[start][res].

    Now you can go through all of the partitions, but also through all of the end and start points and all of the result masks in every half. It works in #(partitions) * 7 * 7 * 2^6 * 2^6.

    It's too long. So we also need to pre-count d[start_mask][res] for the second half of every partition. Here start_mask is a set of vertices where you can start.

    Now once you fixed a fin you don't need to go through every start. You just need to look at the set of adjacent to fin and the set of all others. This will work in #(partitions) * 7 * 2 * 2^6 * 2^6 and gets AC.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In Question D2

"In order to find the longest palindrome which is a prefix of some string, a, let's find p from the prefix function of the string a+ '#' +a¯, where a¯ represents the reverse of a."

Could someone please explain what this means.

Thanks

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +6 Проголосовать: не нравится

    We want to find the longest prefix which is also a palindrome, lets call it string $$$a$$$(the longest palindrome prefix). What do we do now?, we use prefix-function(prefix-function is an algorithm which can find the longest prefix of a string, which is also a suffix of the string and is not equal to the string itself, in $$$O(n)$$$, you can find it in cp-algorithms). Then lets run prefix-function on string $$$s[k+1..n-k]+"|"+rs$$$, where $$$rs$$$ is reverse of string $$$s[k+1..n-k]$$$.

    Why the prefix-function's return is the longest palindrome prefix?, you can understand it yourself!!

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

KMP O(n) solution (Longest palindromic prefix)

https://discuss.interviewbit.com/t/kmp-o-n-solution-longest-palindromic-prefix/29112

Can someone please explain the logic behind the code. Thanks!!

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +1 Проголосовать: не нравится

    Suppose the string $$$S=AB$$$, where $$$A$$$ is a palindrome. Then the code computes the KMP array for $$$S\text{#} S'=AB\text{#} (AB)'=AB\text{#} B'A.$$$ Note that $$$A$$$ is both a prefix and a suffix of the string, and the KMP array computes the longest proper prefix that is also a suffix. That is, the last KMP array value will be at least the length of $$$A$$$.

    Conversely, suppose we have the longest prefix that is also a suffix of the string $$$S\text{#}S'.$$$ Clearly, it cannot contain the $$$\text{#}$$$, because it does not appear elsewhere in the string. So, we have the longest prefix of $$$S$$$ that is a suffix of $$$S'$$$. But these are just the same characters in reverse order, so we must have a palindrome!

»
6 лет назад, скрыть # |
Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

How does CF compile c++14?

I submited problem D

On test2:

case input: q

Real Ouput: qq

my ouput: q$q.

But on my pc my output was like the real output "qq"

I didn't realize that mistake 'til, I saw the real test after contest

Help me pls.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can somebody tell me all the various ways of solving D2? I used string hashing, editorialist used prefix function, did anyone use any other method too?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Isn't 2222...29 also a possible solution for Ugly Bad Numbers?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +20 Проголосовать: не нравится

Case 3 of G: Furthermore, the last edge on the path from $$$i_x$$$ to $$$i_{x+1}$$$ must equal to the first edge on the path from $$$i_{x+1}$$$ to $$$i_{x+2}$$$.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I don't know why a manacher alogrithm for D2 which should be O(n) get TLE. Does anyone know the reason or got AC by using it? Thank you.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Sorry to bother, but why my O(n) solution in problem B got TLE in the system test but got AC just now when I submitted exactly the same code? Could somebody please tell me the reason? Thx a lot. 73666334 73748630

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +14 Проголосовать: не нравится

    There isn't much difference between 982ms and 1000ms. Expect ~30ms difference. It may be due to overload on servers at that moment etc.... You soln is O(n) but input/output takes too much time here. It's close to 2MB (2^21 chars). I added this line for fast input output in your code and it runs in 124ms. 73749515

    Fast Input Output
  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +2 Проголосовать: не нравится

    My code is similar to yours, but mine runs fast at the forth test case 73756081. Why your solution runs so slow is mainly because input/output takes too much time, as @aryanc403 said. In my code, output here is independent:

    rep(i, 1, n) cout << a[i] << " ";
    

    This will take advantage of IO buffers, which makes the compiler happy. To verify my conclusion further, I edited your code to become four versions. The first is your original code:

    cin >> n;
    rep(i, 1, n) {
    	cin >> x;
    	ll now = x + ma;
    	ma = max(now, ma);
    	cout << now << " ";
    }
    cout << endl;
    

    It costs 826 ms at the test#4. 73757350

    I add flush after cout << now << " "; in the second one. It costs 857 ms at test#4. 73757273

    I extract the output part to make it independent in the third version, like my code. It costs 529 ms at test#4. 73756448

    In the fourth version, I flush the IO stream after each outputs, in the basis of the third version. It costs 841 ms at test#4. 73756516

    It is found that independent IO without flush runs fast, but with flush runs slowly; the in-independent IO without explicit flush runs slow, as well as the in-independent IO with explicit flush runs slowly.

    It is shown that the in-independent IO runs flush implicitly, the independent IO does not run flush unless you specified explicitly. I guess the reason for the former is that there are both input and output in the for-loop, which makes the IO stream flush quickly.

    Could someone who is familiar with the internal working mechanism about iostream in C++ tell me more? Thanks a lot.

    • »
      »
      »
      6 лет назад, скрыть # ^ |
      Rev. 3  
      Проголосовать: нравится 0 Проголосовать: не нравится

      flushing the buffer is slow, so you shouldn't use endl for newlines (if there are a lot of them) because they flush. Instead use '\n'. Also adding ios::sync_with_stdio(0); cin.tie(0); at the beginning of your main function will make input faster. I think the input is the problem in your code because mine looks similar and it runs in <200ms.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

The largest bloody lesson I learned from this contest is to use two primes to hash instead of just one prime.

My D2 get WA during system test due to hash collision.

I was able to be in 500th and join the T-shirt lottery but now my rating even drops...

So sad Orz

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +42 Проголосовать: не нравится

A Permutationforces round.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why is the editorial of D2 not given?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

My solution for C was failing on pretest 5. Has any1 faced the same problem?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -44 Проголосовать: не нравится

Useless editorials

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

I like this editorial. It's great.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -92 Проголосовать: не нравится

Why do they give mod = 998 244 353 instead of 1e9 + 7 ?? Got 3 wrong submissions because of that !!! And if they do ...they should have highlighted that !!

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Trie Solution for D1
Link to solution
Approach :- Insert all suffixes in Trie and iterate over all prefixes and search if whole prefix or some part of it is present in Trie and return it's length. If length of suffix is equal to length of prefix then you find one solution and update answer. If it's not then for the remaining part of the prefix which is not found in the suffix of Trie check whether it it is palindrome or not. If it is update the answer.
Same you do with suffixes and insert all prefixs in another Trie. Do the same thing.
Complexity :- O(n^2) (For every prefix or suffix you will iterate at max it's length.)

Though a little long solution but it just clicked in the contest so I wrote it and was accepted.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

If someone can explain approach of question D2 in simple words it would be great help

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 4  
    Проголосовать: нравится +9 Проголосовать: не нравится

    Summary

    • Find the longest matching prefix and suffix, where suffix = palindrome(prefix)
    • Find the longest inner substring that is a palindrome and touches the prefix or suffix (or both)
    • Combine these as final answer

    Steps

    1. From both ends of the string, find a prefix and suffix that are mutual palindromes. Store that for the final answer
    2. Strip off these matching prefix and suffix (if any) so you're left with just the inner string
    3. Run Manacher's algorithm on the inner string
    4. Using the output from step 3, find the max value (i.e. max palindrome length in inner string) that touches the prefix or the suffix (or both).
    5. Return [prefix] + [longest internal palindrome that touches either prefix or suffix] + [suffix]

    Manacher's Algorithm

    • This algorithm finds all palindromes in the string in O(N). It treats each index as a "center" and finds the "radius" (or half length) of the palindrome around that center. It stores this radius for each index in the string. For instance, for "aba", the values returned would be [1, 2, 1]
    • The algorithm starts off with a brute force palindrome search (i.e. from the first point, expand linearly in both directions to see how far the palindrome stretches). Then for the next point, first re-use pre-computed information, if that exists. Then continue linearly expanding to see how much further the palindrome stretches. Store this palindrome length ("radius") for that index.
    • The key observation that's used here is that if the current index lies to the right of a known palindrome and falls within that palindrome's range, then you can simply copy over the pre-computed values from the left half to the right half, and then continue expanding on the right to see how much longer the palindrome on the right stretches. This is because the left half is an exact mirror image of the right.
    • For eg. if the string is "abcdcba", then since we have a palindrome around "abc[d]cba" and both b's fall within its radius, palindrome_len("abcdc[b]a") = palindrome_len("a[b]cdcba") + {any additional palindromic text to the right}
    • Side observation: it's sort of a DP equivalent approach where for any index, you first read what has been partially pre-computed (by virtue of the current index falling under the radius of a prior index), then manually expand beyond that, then save that value, then repeat for next index.

    References

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +1 Проголосовать: не нравится

      Thanks a lot buddy ! Really it was amazing video and tutorial provided by you was quite helpful!!! :)

      • »
        »
        »
        »
        6 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        Sure thing! I've edited it further with an example, and some more clarifications.

        I had a tough time with the last contest (misinterpreted C and spent all my time trying to oversolve it), so I made it a point to try and upsolve a few of the other problems that I wasn't able to get to. D2 required a fair bit of reading to understand how Manacher's algorithm worked, so thought I'd share my notes here for the benefit of anyone who has not heard of this algorithm before. If anyone sees a more efficient way of doing this, I welcome your feedback.

        I've skipped a few details for readability. for eg. there's multiple approaches to it too — you could compute both manacher_odd and manacher_even indexes and use that, or you could modify the input string to insert a special character (for eg. "*") before and after every character in the string, and then do a single manacher run on it (which is what I have used in my input).

        I'm sure there are other ways to find the longest palindrome for D1/D2 (for eg. someone suggested a prefix method?) When I went through submissions of many of the top contestants, they had used manacher, but obviously the other approaches work just as well.

    • »
      »
      »
      6 лет назад, скрыть # ^ |
      Rev. 2  
      Проголосовать: нравится 0 Проголосовать: не нравится

      can you please help me understand why this (https://mirror.codeforces.com/contest/1326/submission/73942810) is giving wrong answer?

      • »
        »
        »
        »
        6 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        I'm not familiar with the prefix function (I used manacher's algorithm instead), but the editorial uses that, and it says:

        In order to find the longest palindrome which is a prefix of some string, a, lets find p from the prefix function of the string a + "#" + reverse(a)

        Are you setting the string accordingly before you send it to the prefix function? Note that you'll need to repeat this for the suffix (per the editorial).

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +1 Проголосовать: не нравится

      Helped me a lot, thanks brother!!

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      if(i-radius[i]+1 <= 0 || i+radius[i]-1 >= n-1)

      can u plz explain this line?? thank u

      • »
        »
        »
        »
        6 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        This is when you're trying to find the longest inner string that is a palindrome and also touches the outer string. Let's say the original string was abcdedxcba. Here the outer string will be abc...cba. Then you want to find the largest inner string that touches either the initial abc part or the latter cba part. So we run manacher's on dedx and find that ded is the longest inner palindrome that touches the abc portion of the outer string. This is because at i=1 (i.e. at the character 'e'), radius[i]=2 (and hence i-radius[i] + 1 = 0).

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can somebody please explain the perfix function in problem D2??

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can someone help me finding the mistake in my logic for problem E. I did similar thing as mentioned in editorial.

After putting ith bomb 'ans' will be valid if for every index 'j' after 'position[ans] in array p' the number of bombs in prefix(starting from position[ans]) <= # elements greater than 'ans' till j — #elements greater than 'ans' till the previous bomb position just before poition[ans].

I implemented it but got WA on TC-4. Can someone please find the mistake? Code: https://mirror.codeforces.com/contest/1326/submission/73769004

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится -16 Проголосовать: не нравится

It's really sad to get the result that I failed on System Test 32 in D2 using hashing.

I use base=233,mod=998244353 in my hashing. I tried to modify the base or mod, both can get accepted. It seems that the test kills the hashing with base=233,mod=998244353. :(

Anyway D is really a great problem. I was writing a messy code using manacher algorithm in contest before I realized it can be solved with hashing. It takes me a long time.

Sorry for my poor English.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

Can anyone share similar problems to D1/D2 related to string searching? It would be really helpful for beginners to practice on this topic.

Thank you!

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

For 1326A — Bad Ugly Numbers, as the solution of the editorial if n = 13 then the output comes 2333333333333. But it is divisible by 3, which doesn't fit those conditions. Then isn't the solution wrong?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

How to Solve problem D1 and D2, Please tell the complete approach.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Can someone please explain D easy version? The approach that I used is as follows:- first check if the given string is a palindrome, if yes, print given string and go for next test case else do the following. 1. set L=1 2. remove all possible substring of length L, one by one and each time check that the remaining string is a palindrome or not. If it is a palindrome then print it, break the loop and go for the next test case. Else continue to check rest substring removals. 3. After checking all substring removal, increase L, that is L++ 4. go to step 2 until you get a palindrome in step 3

The code is as follows:-

t=int(input()) for _ in range(t): s=input() if s==s[::-1]: print(s) else: n=len(s) l=1 flag=True while flag: for i in range(n-l+1): p=s[:i]+s[i+l:] print("p =",p) if p==p[::-1]: print(p) flag=False break l+=1

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

For the problem D1, I used the following approach: Suppose the answer is the string t = a + b, length(a) > length(b), a is prefix, b is suffix, then there is a prefix of a that is same as string b and the remaining part of a is a pallindrome in itself. Similar approach can be used length(**a**) < length(**b**), with the change being that there exist a suffix of b that is the same as a. I'm getting wrong answer on test case 16. Can anyone point out the flaw in this approach? It'll be really helpful.73818028

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

PROBLEM C DOUBT can someone explain me the definition of partition, segment and partition value(in this problem) in layman's term (first a non — mathematical followed by a mathematical)?

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

...

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

I am a python user and it's been almost 24 hours since I have been trying to solve the D1 problem of codeforces global round 7. My code works fine in all the test cases I can think of and can easily handle a string of length 5000. But, I am getting time limit exceeded error in test case 5 of codeforces. Here is my code; if there's any python user out there, please, help me out.

SOURCE CODE:
def palindrome(s,si,ei):

l=len(s)
if si>=ei:
    return True
elif(s[si]==s[ei]):
    return palindrome(s,si+1,ei-1)
else:
    return False

n=int(input())

li=[]

for i in range(n):

li.append(input())

for ele in li:

stm=palindrome(ele,0,len(ele)-1)
if stm:
    print(ele)
else:
    longest=1
    final_str=ele[0]
    for i in range(len(ele)):

        prefix=ele[0:i:1]
        for j in range(i+1,len(ele)+1):

            suffix=ele[j:len(ele):1]
            new_str=prefix+suffix                
            lll=len(new_str)
            stm_1=palindrome(new_str,0,lll-1)
            if stm_1:                    
                if lll>longest:
                    final_str=new_str
                    longest=lll
                    break

    print(final_str)

Again, this code is working for all the test cases the human brain can think of but fails to pass test case 5 of codeforces for reasons which have been tormenting me for a while, since I don't know the reason at all.

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    Unfortunately there is no way of downloading the full test data, but your implementation is O(n^3) so is likely to be slow for large test cases. Also recursion is quite slow in Python, and best avoided as far as possible.

    This can be reduced to O(n^2) by breaking the problem into parts:

    1. find how much of the start of the input string matches the reversed end of the string, this will be the start and reversed end of the result.

    2. find the longest palindrome at the start or end of the middle section of the string and put it in the middle of the result string.

    My solution following this strategy is here

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Can anyone elaborate explanation of E problem ? Thanks in Advance

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Who likes parsing — like

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In C, where did n-k+1 come from?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    So you want the k largest numbers to be in separate partitions so the total partition value is the greatest. Because a is a permutation of numbers from 1 to n, the k largest numbers are n, n-1, n-2 ... n-k+2, n-k+1 (n-k is the k+1th largest number as n-1 is the second largest)

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

isaf27 Can you please share the test generation strategy for Problem D. I was trying to generate test cases during the contest for stress testing and it turned out that even a random string comprising of Just two character say just 'a' and 'b' for $$$N$$$ = $$$2*10^5$$$ had answer almost around 30-50. I was curious to know about the strategy you used to create test cases for actual task because they were really strong.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +6 Проголосовать: не нравится

    It is true, that for random strings the length of the answer isn't big, because you have a very very small probability to get the long string in the answer (there are many pairs of symbols that should be equal). So, in the generation of the random strings, you will never get some extreme test cases.

    About test generation strategy: it's hard to share it because I had some generators, which are not obvious to understand. But some idea of how to generate the string with the long string in the answer: just generate some palindrome first, after that split it randomly and add some symbols between the parts.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

https://www.youtube.com/playlist?list=PLl4Y2XuUavmsf8Os2QTsRgi6Gn5M1dWO8

Uploaded solution videos of problems A — D2 on this link.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

 

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In Problem F2, is there another choice besides using FWT to calculate the convolution of g_ai quickly?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

74680175 Why I am getting TLE in Problem 2 complexity of my soln is O(n^2) I have used += everywhere using string operation and the code is working fine of TC 2 on my laptop but it is giving TLE on cf why?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem D my code is failing on TEST 16 can someone find the test_case for which its failing. My code :https://mirror.codeforces.com/contest/1326/submission/80047216

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Please can someone tell me why I am getting TLE in Prefix — Suffix Palindrome (HARD) I have maintained to hash array one for forward and one for backward Then I first used two — pointers to get the common elements from first and end. Then I checked the largest palindrome that is in prefix and in suffix in the remaining string (after I removed the common elements ) using forward and reverse hash. This is the link to my solution

»
5 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

I don't understand why D2 solution is true. Can anyone prove it?

Mybe we have a smaller k but a longer palindrome that makes answer bigger.

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

seems like editorial solution of D2 didn't include hashing which I used to solve