Statement
You are given two strings a and b. Find shortest string which being repeated infinitely contains the both strings. I.e. find such shortest s that infinite string ss... s... contains a and contains b as a substring.
Constraints
- $$$1 \le Number of Test Cases \le 100$$$
- $$$1 \le len(a) \le 10000$$$
- $$$1 \le len(b) \le 10000$$$
This problem is not from an ongoing contest. Those who have access to the group (Brazil ICPC Summer School 2018) can view it here!
I would follow the next steps:
edit: check that this right string may be repeated string, like a = "dadada", b = "da"
I think that's all. All of this you may do with only prefix function.
Try a = "cabc" and b = "bca" This won't work. Answer is s = "abc"
i think printing all the characters that appears in any one of the string would work
I think this only works for subsequence not substring.
Break the problem into 2 subproblems:
We will solve them separately, both utilize the knowledge of Longest proper prefix/Failure function.
Task 1
Similar Idea — SPOJ — CF25E
Now we will try to form the string S from the above strings T (for T1 and T2, try both and pick the one which gives smaller S)
Task 2
We can consider the string as a circular string, it makes sense to remove the longest proper prefix that is also a suffix from T, then the leftover string when repeated infinite times will have T as the substring. Here we get the required string S.
abcab — LPS = ab, removing it gives "abc" which when repeated does give abc"abcab"c...
babab — LPS = bab, removing it gives "ab" which when repeated gives a"babab"aba...
Same Idea — A topcoder problem "Running Letters"