MotaSanyal's blog

By MotaSanyal, history, 8 months ago, ,

Hi everyone!

I was trying to solve this problem which came in last year ICPC Gwalior-Pune Onsite Round.

My approach :

I first checked whether C is present either in A or in B, if so, then answer will be length(A) + length(B) Otherwise , found the longest prefix of C that occurs as a suffix of A (say, X) and longest suffix of C that occurs as prefix of B (say, Y). Then if X+Y <= length(C), answer will be length(A) + length(B) + (length(C) — (X+Y)) , else if X+Y > length(C), then answer is length(A) + length(B) + (length(C) — max(X,Y)).

The verdict I am getting is Wrong Answer. Can anyone please point out where am I going wrong?

• +5

 » 8 months ago, # | ← Rev. 5 →   +2 This test case should be wrong answer: 1 aab bbb abb Because C may not be composed of the greater suffix of A or the greater prefix of B, however, C may be composed of a piece of A plus some string plus a piece of BIn this case the best solution would be to take the suffix of size 2 of A and the prefix of size 1 of BThe problem can be solved by calculating all possible suffixes of A where C can begin and all prefixes of B where C can endAfter you have these sizes, separate the suffixes of A and the prefixes of B into two sets and then order themThen by binary search or the two pointers method you can search for each suffix of A the largest prefix of B such that the suffix of A plus the prefix of B are less than or equal to the size of CThe answer must then be size of A plus size of B plus size of C minus the highest value of the specific above
•  » » 8 months ago, # ^ |   0 Thanks a lot for the test case and for pointing out my mistake. Thanks a lot :)