Under construction

Still being built. Things may change

String Discrimination

Interview

Time limit2sMemory limit512 MB

Summary
Given a target string S and up to 100 words, decide whether S is a concatenation of words from the list, each reusable any number of times.
Level

Medium4 of 10

Topics
Dynamic programming, String, Hash map
Solved
No attempts yet

Problem

A string SS consisting of lowercase alphabet letters and a list of words AA are given. Write a program that determines whether SS can be formed by concatenating one or more strings from AA without any spaces. A word in AA may be used multiple times.

Input

The first line gives a string SS whose length is at most 100. The second line gives the number of strings in AA, NN (1≤N≤1001 \le N \le 100). The following NN lines give the words in AA, one per line. Each string in AA consists only of lowercase alphabet letters and its length does not exceed 100.

Output

Output 1 if SS can be formed from strings in AA, and 0 otherwise.

Examples1

  1. Example 1

    Input
    softwarecontest
    2
    software
    contest
    
    Expected output
    1