During the study of the Martians Petya clearly understood that the Martians are…
- User
During the study of the Martians Petya clearly understood that the Martians are absolutely lazy. They like to sleep and don't like to wake up.
Imagine a Martian who has exactly n eyes located in a row and numbered from the left to the right from 1 to n. When a Martian sleeps, he puts a patch on each eye (so that the Martian morning doesn't wake him up). The inner side of each patch has an uppercase Latin letter. So, when a Martian wakes up and opens all his eyes he sees a string s consisting of uppercase Latin letters. The string's length is n.
"Ding dong!" — the alarm goes off. A Martian has already woken up but he hasn't opened any of his eyes. He feels that today is going to be a hard day, so he wants to open his eyes and see something good. The Martian considers only m Martian words beautiful. Besides, it is hard for him to open all eyes at once so early in the morning. So he opens two non-overlapping segments of consecutive eyes. More formally, the Martian chooses four numbers a, b, c, d, (1 ≤ a ≤ b < c ≤ d ≤ n) and opens all eyes with numbers i such that a ≤ i ≤ b or c ≤ i ≤ d. After the Martian opens the eyes he needs, he reads all the visible characters from the left to the right and thus, he sees some word.
Let's consider all different words the Martian can see in the morning. Your task is to find out how many beautiful words are among them. Input
The first line contains a non-empty string s consisting of uppercase Latin letters. The strings' length is n (2 ≤ n ≤ 105). The second line contains an integer m (1 ≤ m ≤ 100) — the number of beautiful words. Next m lines contain the beautiful words pi, consisting of uppercase Latin letters. Their length is from 1 to 1000. All beautiful strings are pairwise different.
Output
Print the single integer — the number of different beautiful strings the Martian can see this morning.
Examples
Input
ABCBABA 2 BAAB ABBA
Output
1
Note
Let's consider the sample test. There the Martian can get only the second beautiful string if he opens segments of eyes a = 1, b = 2 and c = 4, d = 5 or of he opens segments of eyes a = 1, b = 2 and c = 6, d = 7.
Assistant
import bisect base = 911382629 mod = 10**18 + 3 s = input().strip() n = len(s) m = int(input()) beautiful_words = [input().strip() for _ in range(m)] max_k = 1000 prefix_hash = [0] * (n + 1) power = [1] * (n + max_k + 1) for i in range(n): prefix_hash[i+1] = (prefix_hash[i] * base + ord(s[i])) % mod for i in range(1, n + max_k + 1): power[i] = (power[i-1] * base) % mod hash_maps = {} for k in range(1, max_k + 1): hash_map = {} if k > n: hash_maps[k] = hash_map continue for i in range(n - k + 1): current_hash = (prefix_hash[i + k] - prefix_hash[i] * power[k]) % mod current_hash = (current_hash + mod) % mod if current_hash not in hash_map: hash_map[current_hash] = [] hash_map[current_hash].append(i) hash_maps[k] = hash_map count = 0 for p in beautiful_words: if len(p) < 2: continue found = False for split_pos in range(1, len(p)): left = p[:split_pos] right = p[split_pos:] k = len(left) m_right = len(right) if k > 1000 or m_right > 1000: continue hash_left = 0 for c in left: hash_left = (hash_left * base + ord(c)) % mod hash_right = 0 for c in right: hash_right = (hash_right * base + ord(c)) % mod if k not in hash_maps or m_right not in hash_maps: continue hm_k = hash_maps[k] hm_m = hash_maps[m_right] if hash_left not in hm_k or hash_right not in hm_m: continue left_starts = hm_k[hash_left] right_starts = hm_m[hash_right] if not left_starts or not right_starts: continue first_left_start = left_starts[0] first_left_end = first_left_start + k - 1 required_right_start = first_left_end + 1 pos = bisect.bisect_left(right_starts, required_right_start) if pos < len(right_starts): start_r = right_starts[pos] if start_r + m_right - 1 < n: found = True break last_right_start = right_starts[-1] required_left_end_max = last_right_start - 1 required_left_start_max = required_left_end_max - k + 1 if required_left_start_max >= 0: pos = bisect.bisect_right(left_starts, required_left_start_max) - 1 if pos >= 0: found = True break if found: count += 1 print(count)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.