This is painful problem, I spent 3 hours to solve, but very educational actually. If you are struggling, than read this: https://codeforces.com/topic/60789/ru9
See https://en.wikipedia.org/wiki/Bloom_filter This made it possible to achieve 0.078 seconds at 3 megabytes. In fact, the "optimal" filter memory is only slightly larger than the total memory for storing two entered strings (300 kilobytes).